home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume10 / hum / part02 < prev    next >
Encoding:
Text File  |  1989-08-08  |  61.2 KB  |  3,113 lines

  1. Path: uunet!rs
  2. From: rs@uunet.UU.NET (Rich Salz)
  3. Newsgroups: comp.sources.unix
  4. Subject: v10i28:  Bill Tuthill's "hum" text concordance package, Part02/03
  5. Message-ID: <475@uunet.UU.NET>
  6. Date: 26 Jun 87 21:48:56 GMT
  7. Organization: UUNET Communications Services, Arlington, VA
  8. Lines: 3102
  9. Approved: rs@uunet.uu.net
  10.  
  11. Submitted by: John Gilmore <hoptoad!gnu>
  12. Mod.Sources: Volume 10, Number 28
  13. Archive-name: hum/Part02
  14.  
  15. : To unbundle, sh this file
  16. echo kwal.c
  17. cat >kwal.c <<'@@@ Fin de kwal.c'
  18. # include <stdio.h>                /* kwal.c (rev3.7) */
  19. # include <ctype.h>
  20.  
  21. usage()            /* print usage and synopsis of options */
  22. {
  23.     puts("Key Word And Line concordance program\t\t(rev3.7)");
  24.     puts("Usage: kwal [-kn -m -wS -fn -sn -r -ln -x -dF + -] filename(s)");
  25.     puts("-kn: keyword is n characters long (defaults to 15)");
  26.     puts("-m : keywords not mapped from upper to lower case");
  27.     puts("-wS: write string S onto id field (use quotes around blanks)");
  28.     puts("-fn: filename (up to n characters) written onto id field");
  29.     puts("-sn: skip n characters of lefthand id field in text and write as id");
  30.     puts("-r : reset linenumber to 1 at beginning of every file");
  31.     puts("-ln: line numbering begins with line n (instead of 1)");
  32.     puts("-x : line numbering is suppressed entirely");
  33.     puts("-d : define punctuation set according to file F");
  34.     puts("+  : the + character indicates cedilla or umlaut");
  35.     puts("-  : read text from standard input (terminal or pipe)");
  36.     exit(1);
  37. }
  38.  
  39. int kwlen = 15;        /* lefthand keyword length */
  40. int kwmap = 1;        /* toggle for mapping keyword to lcase */
  41. int wrtid = 0;        /* toggle write onto id field */
  42. char *idfld;        /* string to write onto id field */
  43. int wrtfnm = 0;        /* toggle writing of filename */
  44. int skipid = 0;        /* skip and write imbedded id field */
  45. long lineno = 1;    /* count line numbers */
  46. int resetno = 0;    /* reset lineno for new file */
  47. int plusm = 0;        /* toggle plus mark to indicate accents */
  48. char punctuation[BUFSIZ] = ",.;:-?!\"()[]{}" ;
  49.  
  50. main(argc,argv)        /* Key Word And Line concordance program */
  51. int argc;
  52. char *argv[];
  53. {
  54.     FILE *fp, *fopen();
  55.     int i;
  56.  
  57.     if (argc == 1)
  58.         usage();
  59.  
  60.     for (i = 1; i < argc; i++)
  61.     {
  62.         if (*argv[i] == '-')
  63.             getflag(argv[i]);
  64.         else if (*argv[i] == '+')
  65.             plusm = 1;
  66.         else if ((fp = fopen(argv[i],"r")) != NULL)
  67.         {
  68.             kwal(fp, argv[i]);
  69.             fclose(fp);
  70.             if (resetno)
  71.                 lineno = 1;
  72.         }
  73.         else  /* cannot open file */
  74.         {
  75.             fprintf(stderr,
  76.             "Kwal cannot access the file: %s\n", argv[i]);
  77.             continue;
  78.         }
  79.     }
  80.     exit(0);
  81. }
  82.  
  83. getflag(f)        /* parses command line to set options */
  84. char *f;
  85. {
  86.     char *pfile;
  87.     long atol();
  88.  
  89.     f++;
  90.     switch(*f++)
  91.     {
  92.         case 'k':
  93.             kwlen = atoi(f);
  94.             break;
  95.         case 'm':
  96.             kwmap = 0;
  97.             break;
  98.         case 'w':
  99.             wrtid = 1;
  100.             idfld = f;
  101.             break;
  102.         case 'f':
  103.             wrtfnm = atoi(f);
  104.             break;
  105.         case 's':
  106.             skipid = atoi(f);
  107.             break;
  108.         case 'r':
  109.             resetno = 1;
  110.             break;
  111.         case 'l':
  112.             lineno = atol(f);
  113.             break;
  114.         case 'x':
  115.             lineno = 0;
  116.             break;
  117.         case 'd':
  118.             pfile = f;
  119.             getpunct(pfile);
  120.             break;
  121.         case NULL:
  122.             kwal(stdin, "Stdin");
  123.             break;
  124.         default:
  125.             fprintf(stderr,
  126.             "Invalid kwal flag: -%s\n", --f);
  127.             exit(1);
  128.             break;
  129.     }
  130. }
  131.  
  132. getpunct(pfile)        /* read user's punctuation from pfile */
  133. char *pfile;
  134. {
  135.     FILE *pfp, *fopen();
  136.     char s[BUFSIZ], *strcpy();
  137.  
  138.     if ((pfp = fopen(pfile, "r")) == NULL)
  139.     {
  140.         fprintf(stderr,
  141.         "Kwal cannot access Punctfile: %s\n", pfile);
  142.         exit(1);
  143.     }
  144.     else
  145.         while (fgets(s, BUFSIZ, pfp))
  146.             strcpy(punctuation, s);
  147. }
  148.  
  149. char line[BUFSIZ];    /* line buffer of text */
  150. int pos;        /* position of current word */
  151. char wrtskip[BUFSIZ];    /* storage for embedded id field */
  152.  
  153. kwal(fp, fname)        /* prints kwal entry for each word */
  154. FILE *fp;
  155. char *fname;
  156. {
  157.     char wd[BUFSIZ];
  158.  
  159.     while (fgets(line, BUFSIZ, fp) != NULL)
  160.     {
  161.         pos = 0;
  162.         if (skipid)
  163.             cp_skipid();
  164.         while (getword(wd))
  165.         {
  166.             pr_keywd(wd);        /* lefthand keyword */
  167.             pr_idfld(fname);    /* identification fld */
  168.             fputs(line, stdout);    /* print context */
  169.         }
  170.         if (lineno)
  171.             lineno++;
  172.     }
  173. }
  174.  
  175. cp_skipid()        /* handle strings of embedded id field */
  176. {
  177.     char lncpy[BUFSIZ];
  178.     int i, j;
  179.  
  180.     strcpy(lncpy, line);
  181.  
  182.     for (i = 0; i < skipid; i++)
  183.         wrtskip[i] = line[i];
  184.     wrtskip[i] = NULL;
  185.  
  186.     for (i = 0, j = skipid; lncpy[j] != NULL; i++, j++)
  187.         line[i] = lncpy[j];
  188.     line[i] = NULL;
  189. }
  190.  
  191. pr_keywd(wd)        /* print keyword, adjusting for backspaces */
  192. char wd[];
  193. {
  194.     char *cp;
  195.     int i, kwbsno = 0;
  196.  
  197.     cp = wd;
  198.     for (i = 0; i < kwlen; i++)
  199.         if (*cp)
  200.         {
  201.             if (*cp == '\b')
  202.                 kwbsno += 2;
  203.             if (plusm && *cp == '+')
  204.                 kwbsno++;
  205.             putchar(*cp++);
  206.         }
  207.         else
  208.             putchar(' ');
  209.     while (kwbsno-- > 0)
  210.         putchar(' ');
  211.     putchar('|');
  212. }
  213.  
  214. pr_idfld(fname)        /* print specified id fields and number */
  215. char *fname;
  216. {
  217.     char *wfile;
  218.     int i;
  219.  
  220.     if (wrtid)
  221.         printf("%s ", idfld);
  222.     if (wrtfnm)
  223.     {
  224.         wfile = fname;
  225.         for (i = 0; i < wrtfnm; i++)
  226.             if (*wfile)
  227.                 putchar(*wfile++);
  228.             else
  229.                 putchar(' ');
  230.         putchar(' ');
  231.     }
  232.     if (skipid)
  233.         printf("%s    ", wrtskip);
  234.     if (lineno)
  235.         printf("%6ld    ", lineno);
  236. }
  237.  
  238. getword(wd)        /* gets next word on each line */
  239. char *wd;
  240. {
  241.     int ln; 
  242.  
  243.     /* skip over blanks */
  244.     while ((*wd = line[pos++]) && isskip(*wd) && *wd != '\n')
  245.         ;
  246.     if (*wd == '\n')
  247.         ln = 0;
  248.     if (kwmap && isupper(*wd))
  249.         *wd = tolower(*wd);
  250.  
  251.     /* load word from line */
  252.     while ((*++wd = line[pos++]) && !isskip(*wd))
  253.     {
  254.         if (kwmap && isupper(*wd))
  255.             *wd = tolower(*wd);
  256.         ln = 1;
  257.     }
  258.     *wd = NULL;
  259.  
  260.     pos--;            /* unget newline character */
  261.     return(ln);        /* true if there is more line */
  262. }
  263.  
  264. isskip(c)        /* function to evaluate punctuation */
  265. char c;
  266. {
  267.     char *ptr;
  268.  
  269.     if (isspace(c))
  270.         return(1);
  271.     for (ptr = punctuation; *ptr != c && *ptr != NULL; ptr++)
  272.         ;
  273.     if (*ptr == NULL)
  274.         return(0);
  275.     else
  276.         return(1);
  277. }
  278. @@@ Fin de kwal.c
  279. echo kwic.c
  280. cat >kwic.c <<'@@@ Fin de kwic.c'
  281. # include <stdio.h>                /* kwic.c (rev3.7) */
  282. # include <ctype.h>
  283. # include <signal.h>
  284.  
  285. usage()            /* print usage and synopsis of options */
  286. {
  287.     puts("Key Word In Context concordance program\t\t(rev3.7)");
  288.     puts("Usage: kwic [-kn -m -wS -fn -r -ln -pn -ic -cn -dF + -] filename(s)");
  289.     puts("-kn: keyword is n characters long (defaults to 15)");
  290.     puts("-m : keywords not mapped from upper to lower case");
  291.     puts("-wS: write string S onto id field (use quotes around blanks)");
  292.     puts("-fn: filename (up to n characters) written onto id field");
  293.     puts("-r : reset linenumber to 1 at beginning of every file");
  294.     puts("-ln: line numbering begins with line n (instead of 1)");
  295.     puts("-pn: page numbering begins with page n (instead of 1)");
  296.     puts("-ic: page incrementer is character c (defaults to =)");
  297.     puts("-cn: context is n characters long (defaults to 50)");
  298.     puts("-d : define punctuation set according to file F");
  299.     puts("+  : the + character indicates cedilla or umlaut");
  300.     puts("-  : read text from standard input (terminal or pipe)");
  301.     puts("Kwic will linenumber until first page indicator.");
  302.     exit(1);
  303. }
  304.  
  305. int kwlen = 15;        /* lefthand keyword length */
  306. int kwmap = 1;        /* toggle mapping of keyword to lcase */
  307. int wrtid = 0;        /* toggle write onto id field */
  308. char *idfld;        /* string to write onto id field */
  309. int wrtfnm = 0;        /* toggle writing of filename */
  310. long lineno = 1;    /* count line numbers */
  311. int resetno = 0;    /* reset lineno for new file */
  312. char pgincr = '=';    /* page incrementing character */
  313. int pageno = 0;        /* toggle and count page numbers */
  314. int cntxt = 50;        /* context width around keyword */
  315. int plusm = 0;        /* toggle plus sign as accent mark */
  316. char punct[BUFSIZ/4]     = ",.;:-?!\"()[]{}" ;
  317. char zerowid[BUFSIZ/4]    = "+" ;
  318.  
  319. main(argc, argv)    /* Key Word In Context concordance program */
  320. int argc;
  321. char *argv[];
  322. {
  323.     FILE *fp, *fopen();
  324.     int i;
  325.  
  326.     if (argc == 1)
  327.         usage();
  328.  
  329.     for (i = 1; i < argc; i++)
  330.     {
  331.         if (*argv[i] == '-')
  332.             getflag(argv[i]);
  333.         else if (*argv[i] == '+')
  334.             plusm = 1;
  335.         else if ((fp = fopen(argv[i], "r")) != NULL)
  336.         {
  337.             kwic(fp, argv[i]);
  338.             fclose(fp);
  339.             if (resetno)
  340.                 lineno = 1;
  341.         }
  342.         else
  343.         {
  344.             fprintf(stderr,
  345.             "Kwic cannot access the file: %s\n", argv[i]);
  346.             continue;
  347.         }
  348.     }
  349.     exit(0);
  350. }
  351.  
  352. getflag(f)        /* parses command line to set options */
  353. char *f;
  354. {
  355.     char *pfile;
  356.     long atol();
  357.  
  358.     f++;
  359.     switch(*f++)
  360.     {
  361.         case 'k':
  362.             kwlen = atoi(f);
  363.             break;
  364.         case 'm':
  365.             kwmap = 0;
  366.             break;
  367.         case 'w':
  368.             wrtid = 1;
  369.             idfld = f;
  370.             break;
  371.         case 'f':
  372.             wrtfnm = atoi(f);
  373.             break;
  374.         case 'r':
  375.             resetno = 1;
  376.             break;
  377.         case 'l':
  378.             lineno = atol(f);
  379.             break;
  380.         case 'p':
  381.             pageno = atoi(f);
  382.             break;
  383.         case 'i':
  384.             pgincr = *f;
  385.             break;
  386.         case 'c':
  387.             cntxt = atoi(f);
  388.             break;
  389.         case 'd':
  390.             pfile = f;
  391.             getpunct(pfile);
  392.             break;
  393.         case NULL:
  394.             rd_stdin();
  395.             break;
  396.         default:
  397.             fprintf(stderr,
  398.             "Invalid kwic flag: -%s\n", --f);
  399.             exit(1);
  400.             break;
  401.     }
  402. }
  403.  
  404. getpunct(pfile)        /* read user's punctuation from pfile */
  405. char *pfile;
  406. {
  407.     FILE *pfp, *fopen();
  408.     char s[BUFSIZ/4], *strcpy();
  409.  
  410.     if ((pfp = fopen(pfile, "r")) == NULL)
  411.     {
  412.         fprintf(stderr,
  413.         "Kwic cannot access Punctfile: %s\n", pfile);
  414.         exit(1);
  415.     }
  416.     if (fgets(s, BUFSIZ/4, pfp))
  417.     {
  418.         strcpy(punct, s);
  419.         punct[strlen(punct)-1] = NULL;
  420.     }
  421.     if (fgets(s, BUFSIZ/4, pfp))
  422.     {
  423.         strcpy(zerowid, s);
  424.         zerowid[strlen(zerowid)-1] = NULL;
  425.         plusm = 1;
  426.     }
  427. }
  428.  
  429. char *tempfile;        /* file storage for seeking stdin */
  430.  
  431. rd_stdin()        /* create tempfile with standard input */
  432. {
  433.     FILE *tfp, *fopen();
  434.     char s[BUFSIZ], *mktemp();
  435.     int catch();
  436.  
  437.     tempfile = "/tmp/KwicXXXXX";
  438.     mktemp(tempfile);
  439.     tfp = fopen(tempfile, "w");
  440.     if (signal(SIGINT, SIG_IGN) != SIG_IGN)
  441.         signal(SIGINT, catch);
  442.     while (fgets(s, BUFSIZ, stdin))
  443.         fputs(s, tfp);
  444.     fclose(tfp);
  445.     tfp = fopen(tempfile, "r");
  446.     kwic(tfp, "Stdin");
  447.     unlink(tempfile);
  448. }
  449.  
  450. catch()            /* remove tempfile in case of interrupt */
  451. {
  452.     unlink(tempfile);
  453.     fprintf(stderr, "\nInterrupt\n");
  454.     exit(1);
  455. }
  456.  
  457. kwic(fp, fname)        /* prints kwic entry for each word */
  458. FILE *fp;
  459. char *fname;
  460. {
  461.     char word[BUFSIZ], str[BUFSIZ];
  462.     long pos, ftell();
  463.     register int wdlen, i;
  464.  
  465.     while (wdlen = getword(word, fp))
  466.     {
  467.         pr_keywd(word);        /* lefthand keyword */
  468.         pr_idfld(fname);    /* identificaton field */
  469.  
  470.         pos = ftell(fp);        /* where in text? */
  471.         if (pos < cntxt/2 + wdlen + 2)    /* if at beginning */
  472.         {
  473.             fseek(fp, -(pos), 1);
  474.             for (i = 0; i < cntxt/2 - pos + wdlen + 2; i++)
  475.                 putchar(' ');
  476.             load_str(str, (int)(pos - wdlen - 1), 1, fp);
  477.         }
  478.         else            /* if past beginning of text */
  479.         {
  480.             fseek(fp, (long) -(cntxt/2 + wdlen + 2), 1);
  481.             load_str(str, (cntxt/2 + 1), 0, fp);
  482.         }
  483.         printf("%s|", str);    /* print out first half */
  484.  
  485.         end_entry(fp);        /* print out second half */
  486.  
  487.         fseek(fp, pos, 0);    /* back to original location */
  488.         putchar('\n');        /* end concordance entry */
  489.     }
  490. }
  491.  
  492. pr_keywd(word)        /* print keyword, adjusting for backspaces */
  493. char word[];
  494. {
  495.     char *cp, *index();
  496.     register int i, kwbsno = 0;
  497.  
  498.     cp = word;
  499.  
  500.     for (i = 0; i < kwlen; i++)
  501.     {
  502.         if (*cp)
  503.         {
  504.             if (*cp == '\b')
  505.                 kwbsno += 2;
  506.             if (plusm && index(zerowid, *cp))
  507.                 kwbsno++;
  508.             putchar(*cp++);
  509.         }
  510.         else
  511.             putchar(' ');
  512.     }
  513.     while (kwbsno-- > 0)
  514.         putchar(' ');
  515.     putchar('|');
  516. }
  517.  
  518.  
  519. pr_idfld(fname)        /* print specified id fields and numbering */
  520. char *fname;
  521. {
  522.     char *wfile;
  523.     int i;
  524.  
  525.     if (wrtid)
  526.         printf("%s ", idfld);
  527.     if (wrtfnm)
  528.     {
  529.         wfile = fname;
  530.         for (i = 0; i < wrtfnm; i++)
  531.             if (*wfile)
  532.                 putchar(*wfile++);
  533.             else
  534.                 putchar(' ');
  535.         putchar(' ');
  536.     }
  537.     if (pageno)
  538.         printf("%3d,%2ld|", pageno, lineno);
  539.     else
  540.         printf("%6ld|", lineno);
  541. }
  542.  
  543. load_str(str, max, prnt, fp)    /* load first half onto string */
  544. char str[];
  545. int max, prnt;
  546. FILE *fp;
  547. {
  548.     int i, bsno = 0;
  549.     int c;
  550.  
  551.     for (i = 0; i < max; i++)
  552.     {
  553.         c = getc(fp);
  554.         if (!prnt)
  555.         {
  556.             str[i] = ' ';
  557.             prnt = isspace(c);
  558.         }
  559.         else    /* if (prnt) */
  560.         {
  561.             if (c == '\n')
  562.                 str[i] = '/';
  563.             else if (c == '\t')
  564.                 str[i] = ' ';
  565.             else
  566.                 str[i] = c;
  567.  
  568.             if (c == '\b')
  569.                 bsno += 2;
  570.             if (plusm && index(zerowid, c))
  571.                 bsno++;
  572.         }
  573.     }
  574.     str[i] = NULL;
  575.     while (bsno-- > 0)    /* adjust for backspaces */
  576.         putchar(' ');
  577. }
  578.  
  579. end_entry(fp)        /* read and write to end of entry */
  580. FILE *fp;
  581. {
  582.     register int i, c;
  583.  
  584.     for (i = 0; i < cntxt/2 - 2; i++)
  585.     {
  586.         c = getc(fp);
  587.         if (c == '\n')
  588.             c = '/';
  589.         if (c == '\t')
  590.             c = ' ';
  591.         if (c == EOF)
  592.             return;
  593.         putchar(c);
  594.     }
  595.     while (!isspace(c = getc(fp)) && c != EOF)
  596.         putchar(c);
  597. }
  598.  
  599. getword(word, fp)    /* drives program through text word by word */
  600. char word[];
  601. FILE *fp;
  602. {
  603.     static char nl = 0;
  604.     register int wlen = 1;
  605.  
  606.     if (nl)        /* increments lineno at beginning of line */
  607.     {
  608.         nl = 0;
  609.         lineno++;
  610.     }
  611.     while ((*word = getc(fp)) && isskip(*word) && *word != EOF)
  612.         if (*word == '\n')    /* skip to text */
  613.             lineno++;
  614.  
  615.     if (*word == pgincr)
  616.     {
  617.         pageno++;    /* pgincr must begin word */
  618.         lineno = 0;
  619.     }
  620.     if (*word == EOF)
  621.         return(NULL);
  622.     if (kwmap)
  623.         if (isupper(*word))
  624.             *word = tolower(*word);
  625.  
  626.     while ((*++word = getc(fp)) && !isskip(*word) && *word != EOF)
  627.     {                /* get next word */
  628.         if (kwmap)
  629.             if (isupper(*word))
  630.                 *word = tolower(*word);
  631.         wlen++;
  632.     }
  633.     if (*word == '\n')    /* set nl at end of line */
  634.         nl = 1;
  635.     *word = NULL;
  636.  
  637.     return(wlen);
  638. }
  639.  
  640. isskip(c)        /* function to evaluate punctuation */
  641. char c;
  642. {
  643.     char *ptr;
  644.  
  645.     if (isspace(c))
  646.         return(1);
  647.     for (ptr = punct; *ptr != c && *ptr != NULL; ptr++)
  648.         ;
  649.     if (*ptr == NULL)
  650.         return(0);
  651.     else
  652.         return(1);
  653. }
  654. @@@ Fin de kwic.c
  655. echo lno.c
  656. cat >lno.c <<'@@@ Fin de lno.c'
  657. # include <stdio.h>            /* lno.c (rev3.7) */
  658.  
  659. long lno = 1;
  660. int dbles = 0;        /* toggle option for double line numbering */
  661. int strph = 0;        /* toggle and count for strophe numbering */
  662.  
  663. main(argc, argv)    /* line number text, allowing setting */
  664. int argc;
  665. char *argv[];
  666. {
  667.     FILE *fp, *fopen();
  668.     long atol();
  669.     int i;
  670.  
  671.     if (argc == 1)
  672.     {
  673.         puts("Usage: lno [+n -d -h -sn -] filename(s)\t\t(rev3.7)");
  674.         puts("+n : the beginning line number is n, not 1");
  675.         puts("-d : double line number text with long lines");
  676.         puts("-h : hemistich number text with split lines");
  677.         puts("-sn: number and letter strophes of n lines");
  678.         puts("-  : read standard input instead of files");
  679.         exit(1);
  680.     }
  681.     for (i = 1; i < argc; i++)
  682.     {
  683.         if (*argv[i] == '+')
  684.             lno = atol(argv[i]+1);
  685.         else if (*argv[i] == '-')
  686.             getflag(argv[i]);
  687.         else if ((fp = fopen(argv[i], "r")) != NULL)
  688.         {
  689.             if (dbles)
  690.                 dblno(fp);
  691.             if (strph)
  692.                 strfno(fp);
  693.             else  /* default action */
  694.                 lineno(fp);
  695.             fclose(fp);
  696.         }
  697.         else  /* attempt to open file failed */
  698.         {
  699.             fprintf(stderr,
  700.             "Lno cannot access the file: %s\n", argv[i]);
  701.             continue;
  702.         }
  703.     }
  704.     exit(0);
  705. }
  706.  
  707. getflag(f)        /* parses command line to set options */
  708. char *f;
  709. {
  710.     f++;
  711.     switch(*f)
  712.     {
  713.         case 'd':
  714.             dbles = 1;
  715.             break;
  716.         case 'h':
  717.             strph = 2;
  718.             break;
  719.         case 's':
  720.             strph = atoi(++f);
  721.             if (strph > 26)
  722.             {
  723.                 fprintf(stderr,
  724.                 "Too many lines per strophe\n");
  725.                 exit(1);
  726.             }
  727.             break;
  728.         case NULL:
  729.             if (dbles)
  730.                 dblno(stdin);
  731.             if (strph)
  732.                 strfno(stdin);
  733.             else
  734.                 lineno(stdin);
  735.             break;
  736.         default:
  737.             fprintf(stderr, "Invalid lno flag: -%s\n", f);
  738.             exit(1);
  739.             break;
  740.     }
  741. }
  742.  
  743. lineno(fp)        /* this function is self-explanatory */
  744. FILE *fp;
  745. {
  746.     char s[BUFSIZ];
  747.  
  748.     while (fgets(s, BUFSIZ, fp))
  749.     {
  750.         printf("%6ld\t", lno);
  751.         fputs(s, stdout);
  752.         lno++;
  753.     }
  754. }
  755.  
  756. dblno(fp)        /* number text, incrementing by twos */
  757. FILE *fp;
  758. {
  759.     char s[BUFSIZ];
  760.  
  761.     while (fgets(s, BUFSIZ, fp))
  762.     {
  763.         printf("%6ld\t", lno);
  764.         fputs(s, stdout);
  765.         lno += 2;
  766.     }
  767. }
  768.  
  769. strfno(fp)        /* number n line strophes, appending letters */
  770. FILE *fp;
  771. {
  772.     char s[BUFSIZ], ch = 'a';
  773.     int i = 0;
  774.  
  775.     while (fgets(s, BUFSIZ, fp))
  776.     {
  777.         printf("%5ld%c\t", lno, ch);
  778.         fputs(s, stdout);
  779.         if (++i < strph)
  780.             ch++;
  781.         else
  782.         {
  783.             lno++;
  784.             i = 0;
  785.             ch = 'a';
  786.         }
  787.     }
  788. }
  789. @@@ Fin de lno.c
  790. echo maxwd.c
  791. cat >maxwd.c <<'@@@ Fin de maxwd.c'
  792. # include <stdio.h>            /* maxwd.c (rev3.7) */
  793. # include <ctype.h>
  794. # define LNGBUF (BUFSIZ * 2)
  795.  
  796. char filename[15], mxword[BUFSIZ], mxline[LNGBUF];
  797. int mxwdlen = 0, lineno = 0;
  798. int mxlnlen = 0, doline = 0;
  799.  
  800. main(argc, argv)    /* find, measure and print longest word */
  801. int argc;
  802. char *argv[];
  803. {
  804.     FILE *fopen(), *fp;
  805.     int i;
  806.  
  807.     if (argc == 1)
  808.     {
  809.         puts("Usage: maxwd [-l -dF -] filename(s)\t\t(rev3.7)");
  810.         puts("will give file, linenumber, maximum word length,");
  811.         puts("\tand then print the longest word");
  812.         puts("-l: look for longest line instead of longest word");
  813.         puts("-d: define punctuation set according to file F");
  814.         puts("- : read standard input instead of files");
  815.         exit(1);
  816.     }
  817.     for (i = 1; i < argc; i++)
  818.     {
  819.         if (*argv[i] == '-')
  820.             getflag(argv[i]);
  821.         else if ((fp = fopen(argv[i], "r")) != NULL)
  822.         {
  823.             if (doline)
  824.                 maxline(fp, argv[i]);
  825.             else
  826.                 maxword(fp, argv[i]);
  827.             fclose(fp);
  828.         }
  829.         else  /* cannot open file */
  830.         {
  831.             fprintf(stderr,
  832.             "Maxwd cannot access the file: %s\n", argv[i]);
  833.             exit(1);
  834.         }
  835.     }
  836.     printf("%s: line %d: ", filename, lineno);
  837.     if (doline)
  838.     {
  839.         printf("%d characters:\n", mxlnlen-1);
  840.         printf("%s", mxline);
  841.     }
  842.     else
  843.     {
  844.         printf("%d characters:\n", mxwdlen);
  845.         printf("%s\n", mxword);
  846.     }
  847.     exit(0);
  848. }
  849.  
  850. getflag(f)        /* parses command line to set options */
  851. char *f;
  852. {
  853.     char *pfile;
  854.  
  855.     f++;
  856.     switch(*f++)
  857.     {
  858.         case 'l':
  859.             doline = 1;
  860.             break;
  861.         case 'd':
  862.             pfile = f;
  863.             getpunct(pfile);
  864.             break;
  865.         case NULL:
  866.             if (doline)
  867.                 maxline(stdin, "Stdin");
  868.             else
  869.                 maxword(stdin, "Stdin");
  870.             break;
  871.         default:
  872.             fprintf(stderr,
  873.             "Invalid maxwd flag: -%s\n", --f);
  874.             exit(1);
  875.             break;
  876.     }
  877. }
  878.  
  879. char punct[BUFSIZ] = ",.;:-?!\"()[]{}" ;
  880.  
  881. getpunct(pfile)        /* read user's punctuation from pfile */
  882. char *pfile;
  883. {
  884.     FILE *pfp, *fopen();
  885.     char s[BUFSIZ], *strcpy();
  886.  
  887.     if ((pfp = fopen(pfile, "r")) == NULL)
  888.     {
  889.         fprintf(stderr,
  890.         "Maxwd cannot access Punctfile: %s\n", pfile);
  891.         exit(1);
  892.     }
  893.     else
  894.         while (fgets(s, BUFSIZ, pfp))
  895.             strcpy(punct, s);
  896. }
  897.  
  898. maxword(fp, fname)    /* inspect each file for longest word */
  899. FILE *fp;
  900. char fname[];
  901. {
  902.     char word[BUFSIZ];
  903.     int c, i = 0, wdlen = 0, lno = 1;
  904.  
  905.     while ((c = getc(fp)) != EOF)
  906.     {
  907.         if (!isskip(c))
  908.         {
  909.             word[i++] = c;
  910.             wdlen++;
  911.         }
  912.         else  /* if (isskip(c)) */
  913.         {
  914.             word[i] = NULL;
  915.             i = 0;
  916.             if ((wdlen = strlen(word)) > mxwdlen)
  917.             {
  918.                 strcpy(filename, fname);
  919.                 lineno = lno;
  920.                 mxwdlen = wdlen;
  921.                 strcpy(mxword, word);
  922.             }
  923.         }
  924.         if (c == '\n')
  925.             lno++;
  926.     }
  927. }
  928.  
  929. isskip(c)        /* function to evaluate punctuation */
  930. char c;
  931. {
  932.     char *ptr;
  933.  
  934.     if (isspace(c))
  935.         return(1);
  936.     for (ptr = punct; *ptr != c && *ptr != NULL; ptr++)
  937.         ;
  938.     if (*ptr == NULL)
  939.         return(0);
  940.     else
  941.         return(1);
  942. }
  943.  
  944. maxline(fp, fname)    /* inspect each file for longest line */
  945. FILE *fp;
  946. char fname[];
  947. {
  948.     char line[LNGBUF];
  949.     int lnlen = 0, lno = 0;
  950.  
  951.     while (fgets(line, LNGBUF, fp))
  952.     {
  953.         lno++;
  954.         if ((lnlen = strlen(line)) > mxlnlen)
  955.         {
  956.             strcpy(filename, fname);
  957.             lineno = lno;
  958.             mxlnlen = lnlen;
  959.             strcpy(mxline, line);
  960.         }
  961.     }
  962. }
  963. @@@ Fin de maxwd.c
  964. echo med.c
  965. cat >med.c <<'@@@ Fin de med.c'
  966. # include <curses.h>
  967. # include <signal.h>
  968.  
  969. main()            /* med - a full-screen music editor */
  970. {
  971.     int y, x, i, die();
  972.     char c, cmd;
  973.  
  974.     initscr();
  975.     signal(SIGINT, die);
  976.     crmode();
  977.     noecho();
  978.     nonl();
  979.  
  980.     paintscr();
  981.     while (1)
  982.     {
  983.         c = getch();
  984.         if (c == '\r')        /* RETURN */
  985.             move(y+1, 0);
  986.         else if (c == '\012')    /* ^J, nl */
  987.             move(y+1, x);
  988.         else if (c == '\013')    /* ^K, vt */
  989.             move(y-1, x);
  990.         else if (c == '\014')    /* ^L, np */
  991.             move(y, x+1);
  992.         else if (c == *HO)    /* HOME */
  993.         {
  994.             for (i = 1; HO[i] != '\0' && c == HO[i-1]; i++)
  995.                 c = getch();
  996.             move(0, 0);
  997.         }
  998.         else if (c == '|')    /* staff */
  999.         {
  1000.             addch(c);
  1001.             for (i = 1; i < 3; i++)
  1002.             {
  1003.                 move(y-i, x);
  1004.                 addch(c);
  1005.             }
  1006.         }
  1007.         else if (c == '!')    /* bar */
  1008.         {
  1009.             addch(c);
  1010.             for (i = 1; i < 8; i++)
  1011.             {
  1012.                 move(y-i, x);
  1013.                 addch(c);
  1014.             }
  1015.         }
  1016.         else if (c == '\022')    /* redraw screen */
  1017.             paintscr();
  1018.         else
  1019.             addch(c);
  1020.  
  1021.         refresh();
  1022.         getyx(stdscr, y, x);
  1023.         if (y == 0 && x == 0)
  1024.         {
  1025.             cmd = getch();
  1026.             addch(cmd);
  1027.             execute(cmd);
  1028.         }
  1029.     }
  1030. }
  1031.  
  1032. die()            /* restore terminal in case of interrupt */
  1033. {
  1034.     signal(SIGINT, SIG_IGN);
  1035.     mvcur(0, COLS-1, LINES-1, 0);
  1036.     endwin();
  1037.     exit(1);
  1038. }
  1039.  
  1040. paintscr()        /* place treble and bass staffs on screen */
  1041. {
  1042.     register int ln, co;
  1043.     char done = 0;
  1044.  
  1045.     move(0, 0);
  1046.     addstr("_         \n");
  1047. dostaff:
  1048.     addch('\n');
  1049.     addch('\n');
  1050.     for (ln = 1; ln <= 5; ln++)
  1051.     {
  1052.         for (co = 0; co < COLS-1; co++)
  1053.             addch('_');
  1054.         addch('\n');
  1055.         for (co = 0; co < COLS-1; co++)
  1056.             addch(' ');
  1057.         addch('\n');
  1058.     }
  1059.     if (!done && LINES >= 24)
  1060.     {
  1061.         done = 1;
  1062.         goto dostaff;
  1063.     }
  1064.     move(0, 0);
  1065.     refresh();
  1066. }
  1067.  
  1068. execute(cmd)
  1069. char cmd;
  1070. {
  1071.     move(0, 0);
  1072.     printw("Executing %c", cmd);
  1073.     move(0, 0);
  1074.     refresh();
  1075.     if (cmd == 'q')
  1076.     {
  1077.         mvcur(0, COLS-1, LINES-1, 0);
  1078.         endwin();
  1079.         exit(0);
  1080.     }
  1081. }
  1082. @@@ Fin de med.c
  1083. echo pair.c
  1084. cat >pair.c <<'@@@ Fin de pair.c'
  1085. # include <stdio.h>                /* pair.c (rev3.7) */
  1086.  
  1087. main(argc, argv)    /* pair two files side by side (or intercalate) */
  1088. int argc;
  1089. char *argv[];
  1090. {
  1091.     FILE *fopen(), *fp1, *fp2;
  1092.     char s1[BUFSIZ], s2[BUFSIZ], fmt1[15], fmt2[15], *sprintf();
  1093.     int eof1, eof2, len1 = 40, len2 = 40;
  1094.     register int i;
  1095.  
  1096.     if (argc < 3 || argc > 5)
  1097.     {
  1098.         puts("Usage: pair [-m] file1 file2 [+len1 [+len2]]\t(rev3.7)");
  1099.         puts("-m: merge (intercalate) two files line by line");
  1100.         puts("- : read standard input instead of either file");
  1101.         puts("len1 and 2 denote screen width of file1 and 2");
  1102.         exit(1);
  1103.     }
  1104.     if (argv[1][0] == '-' && argv[1][1] == 'm')
  1105.     {
  1106.         merge(argv[2], argv[3]);
  1107.         exit(0);
  1108.     }
  1109.     if (argv[1][0] == '-' && argv[1][1] == '\0')
  1110.         fp1 = stdin;
  1111.     else if ((fp1 = fopen(argv[1], "r")) == NULL)
  1112.     {
  1113.         fprintf(stderr,
  1114.         "Pair cannot access the file: %s\n", argv[1]);
  1115.         exit(1);
  1116.     }
  1117.     if (argv[2][0] == '-' && argv[2][1] == '\0')
  1118.         fp2 = stdin;
  1119.     else if ((fp2 = fopen(argv[2], "r")) == NULL)
  1120.     {
  1121.         fprintf(stderr,
  1122.         "Pair cannot access the file: %s\n", argv[2]);
  1123.         exit(1);
  1124.     }
  1125.     if (argc > 3)
  1126.         len1 = atoi(argv[3]+1);
  1127.     if (argc > 4)
  1128.         len2 = atoi(argv[4]+1);
  1129.     sprintf(fmt1, "%%-%d.%ds", len1, len1 - 1);
  1130.     sprintf(fmt2, "%%-.%ds\n", len2 - 1);
  1131.  
  1132.     eof1 = eof2 = 0;
  1133.     while (!eof1 || !eof2)
  1134.     {
  1135.         if (fgets(s1, BUFSIZ, fp1))
  1136.         {
  1137.             s1[strlen(s1)-1] = '\0';
  1138.             printf(fmt1, s1);
  1139.         }
  1140.         else    eof1 = 1;
  1141.  
  1142.         if (fgets(s2, BUFSIZ, fp2))
  1143.         {
  1144.             if (eof1)
  1145.                 for (i = 0; i < len1; i++)
  1146.                     putchar(' ');
  1147.             s2[strlen(s2)-1] = '\0';
  1148.             printf(fmt2, s2);
  1149.         }
  1150.         else    eof2 = 1;
  1151.  
  1152.         if (eof2 && !eof1)
  1153.             putchar('\n');
  1154.     }
  1155.     exit(0);
  1156. }
  1157.  
  1158. merge(arg1, arg2)    /* intercalate two files line by line */
  1159. char arg1[], arg2[];
  1160. {
  1161.     FILE *fopen(), *fp1, *fp2;
  1162.     char s1[512], s2[512];
  1163.     int eof1, eof2, std = 0;
  1164.  
  1165.     if (arg1[0] == '-' && arg1[1] == NULL)
  1166.     {
  1167.         fp1 = stdin;
  1168.         std = 1;
  1169.     }
  1170.     else if ((fp1 = fopen(arg1, "r")) == NULL)
  1171.     {
  1172.         fprintf(stderr, "Pair cannot access the file: %s\n", arg1);
  1173.         exit(1);
  1174.     }
  1175.     if (arg2[0] == '-' && arg2[1] == NULL)
  1176.     {
  1177.         fp2 = stdin;
  1178.         std = 1;
  1179.     }
  1180.     else if ((fp2 = fopen(arg2, "r")) == NULL)
  1181.     {
  1182.         fprintf(stderr, "Pair cannot access the file: %s\n", arg2);
  1183.         exit(1);
  1184.     }
  1185.     eof1 = eof2 = 0;
  1186.     while (!eof1 || !eof2)
  1187.     {
  1188.         if (fgets(s1, 512, fp1))
  1189.         {
  1190.             fputs(s1, stdout);
  1191.             if (!isatty(fileno(stdout)) && std)
  1192.                 fputs(s1, stderr);
  1193.         }
  1194.         else
  1195.             eof1 = 1;
  1196.  
  1197.         if (fgets(s2, 512, fp2))
  1198.         {
  1199.             if (eof1)
  1200.                 putchar('\n');
  1201.             fputs(s2, stdout);
  1202.             if (!isatty(fileno(stdout)) && std)
  1203.                 fputs(s2, stderr);
  1204.         }
  1205.         else
  1206.             eof2 = 1;
  1207.  
  1208.         if (eof2 && !eof1)
  1209.             putchar('\n');
  1210.     }
  1211. }
  1212. @@@ Fin de pair.c
  1213. echo pause.c
  1214. cat >pause.c <<'@@@ Fin de pause.c'
  1215. # include <stdio.h>            /* pause.c (rev3.7) */
  1216.  
  1217. main(argc, argv)    /* stop IBM terminal to change ball */
  1218. int argc;
  1219. char *argv[];
  1220. {
  1221.     FILE *fp, *fopen();
  1222.  
  1223.     if (argc == 1)
  1224.         pawse(stdin);
  1225.     else
  1226.     {
  1227.         while (--argc > 0)
  1228.             if ((fp = fopen(*++argv, "r")) != NULL)
  1229.             {
  1230.                 pawse(fp);
  1231.                 fclose(fp);
  1232.             }
  1233.             else  /* attempt to open failed */
  1234.             {
  1235.                 fprintf(stderr,
  1236.                 "Pause cannot access the file: %s\t\t(rev3.7)\n",
  1237.                 *argv);
  1238.                 continue;
  1239.             }
  1240.         exit(0);
  1241.     }
  1242. }
  1243.  
  1244. pawse(fp)        /* pause when ctrl-p is read from file */
  1245. FILE *fp;
  1246. {
  1247.     register int c;
  1248.  
  1249.     while ((c = getc(fp)) != EOF)
  1250.     {
  1251.         if (c == '\020')    /* \020 is ctrl-p */
  1252.             patient();
  1253.         else
  1254.             putchar(c);
  1255.     }
  1256. }
  1257.  
  1258. patient()        /* wait for ctrl-d from terminal */
  1259. {
  1260.     FILE *termptr, *fopen();
  1261.     int sig;
  1262.  
  1263.     termptr = fopen("/dev/tty", "r");
  1264.     while ((sig = getc(termptr)) != EOF)
  1265.     {
  1266.         if (sig != '\004')    /* \004 is ctrl-d */
  1267.             ;
  1268.         else
  1269.             return;
  1270.     }
  1271.     fclose(termptr);
  1272.     return;
  1273. }
  1274. @@@ Fin de pause.c
  1275. echo revconc.c
  1276. cat >revconc.c <<'@@@ Fin de revconc.c'
  1277. # include <stdio.h>            /* revconc.c (3.7) */
  1278. # include <ctype.h>
  1279.  
  1280. main(argc, argv)    /* reverse concordance module */
  1281. int argc;
  1282. char *argv[];
  1283. {
  1284.  
  1285.     FILE *fp, *fopen();
  1286.  
  1287.     if (argc == 1)
  1288.         revconc(stdin);
  1289.     else
  1290.     {
  1291.         while (--argc > 0)
  1292.             if ((fp = fopen(*++argv, "r")) == NULL)
  1293.             {
  1294.                 fprintf(stderr,
  1295.                 "Revconc cannot access the file: %s\t\t(rev3.7)\n",
  1296.                 *argv);
  1297.                 continue;
  1298.             }
  1299.             else
  1300.             {
  1301.                 revconc(fp);
  1302.                 fclose(fp);
  1303.             }
  1304.         exit(0);
  1305.     }
  1306. }
  1307.  
  1308. revconc(fp)         /* reverse first word (keyword) on each line */
  1309. FILE *fp;
  1310. {
  1311.     char line[512];
  1312.  
  1313.     while (fgets(line, 512, fp))
  1314.     {
  1315.         revwd(line);
  1316.         fputs(line, stdout);
  1317.     }
  1318. }
  1319.  
  1320. revwd(s)          /* reverse first word of string s in place */
  1321. char s[];
  1322. {
  1323.     int i, j;
  1324.     char c;
  1325.  
  1326.     for (i = 0, j = firstwdlen(s) - 1; i < j; i++, j--)
  1327.     {
  1328.         c = s[i];
  1329.         s[i] = s[j];
  1330.         s[j] = c;
  1331.     }
  1332.     return;
  1333. }
  1334.  
  1335. firstwdlen(s)         /* return length of first word in string s */
  1336. char *s;
  1337. {
  1338.     int i = 0;
  1339.  
  1340.     while (!isspace(*s++))
  1341.         i++;
  1342.     return(i);
  1343. }
  1344. @@@ Fin de revconc.c
  1345. echo sfind.c
  1346. cat >sfind.c <<'@@@ Fin de sfind.c'
  1347. # include <stdio.h>            /* sfind.c (rev3.7) */
  1348. # include <ctype.h>
  1349. # define LNGBUF (BUFSIZ*8)
  1350. # define isperiod(c)    (c == '.' || c == '?' || c == '!')
  1351.  
  1352. usage()            /* print usage and synopsis of options */
  1353. {
  1354.     puts("Find sentence (record) matching pattern\t\t\t(rev3.7)");
  1355.     puts("Usage: sfind [-sC -ln -pn -ic -r] 'pattern' [-] filename(s)");
  1356.     puts("-sC: record separator is C (or empty line with no C)");
  1357.     puts("-ln: line number set to n (instead of 1)");
  1358.     puts("-pn: page number set to n (default off)");
  1359.     puts("-ic: page incrementing character is c (not =)");
  1360.     puts("-r : reset linenumber to 1 with each new file");
  1361.     puts(" - : read standard input instead of files");
  1362.     puts("metacharacters:");
  1363.     puts("_ is any single character");
  1364.     puts("* any number of characters");
  1365.     puts("$ matches a newline");
  1366.     puts("\\ escape magic");
  1367.     exit(1);
  1368. }
  1369.  
  1370. char pattern[BUFSIZ];    /* pattern to be searched for */
  1371. int recds = 0;        /* toggle switch for record searching */
  1372. char sepc = NULL;    /* record separating character */
  1373. long lineno = 1;    /* count of line numbers */
  1374. int pageno = 0;        /* toggle and count page numbers */
  1375. char pgincr = '=';    /* page incrementing character */
  1376. int resetno = 0;    /* reset linenumber for new file */
  1377.  
  1378. main(argc, argv)    /* find all sentences matching a pattern */
  1379. int argc;
  1380. char *argv[];
  1381. {
  1382.     FILE *fp, *fopen();
  1383.     int i, apat = 0;
  1384.     char *strcpy();
  1385.  
  1386.     if (argc < 3)
  1387.         usage();
  1388.     for (i = 1; i < argc; i++)
  1389.     {
  1390.         if (*argv[i] == '-')
  1391.             getflag(argv[i]);
  1392.         else if (!apat)
  1393.         {
  1394.             strcpy(pattern, argv[i]);
  1395.             apat = 1;
  1396.         }
  1397.         else if ((fp = fopen(argv[i], "r")) != NULL)
  1398.         {
  1399.             findsent(fp, argv[i]);
  1400.             fclose(fp);
  1401.         }
  1402.         else  /* attempt to open file failed */
  1403.         {
  1404.             fprintf(stderr,
  1405.             "Sfind cannot access the file: %s\n", argv[i]);
  1406.             continue;
  1407.         }
  1408.     }
  1409.     exit(0);
  1410. }
  1411.  
  1412. getflag(f)        /* parses command line to set options */
  1413. char *f;
  1414. {
  1415.     long atol();
  1416.  
  1417.     f++;
  1418.     switch(*f++)
  1419.     {
  1420.         case 's':
  1421.             recds = 1;
  1422.             sepc = *f;
  1423.             break;
  1424.         case 'l':
  1425.             lineno = atol(f);
  1426.             break;
  1427.         case 'p':
  1428.             pageno = atoi(f);
  1429.             break;
  1430.         case 'i':
  1431.             pgincr = *f;
  1432.             break;
  1433.         case 'r':
  1434.             resetno = 1;
  1435.             break;
  1436.         case NULL:
  1437.             findsent(stdin, "Stdin");
  1438.             break;
  1439.         default:
  1440.             fprintf(stderr, "Invalid sfind flag: -%s\n", --f);
  1441.             exit(1);
  1442.             break;
  1443.     }
  1444. }
  1445.  
  1446. findsent(fp, fname)    /* searches through text sentence by sentence */
  1447. FILE *fp;
  1448. char fname[15];
  1449. {
  1450.     char sent[LNGBUF];
  1451.     int more;
  1452.  
  1453.     /* do loop used in case last record has no final sepc */
  1454.     do {
  1455.         more = getsent(sent, LNGBUF, fp);
  1456.         if (index(sent, pattern) >= 0)
  1457.         {
  1458.             printf("===< %s >", fname);
  1459.             if (pageno)
  1460.                 printf("===< page %d >", pageno);
  1461.             printf("===< line %ld >", lineno);
  1462.             printf("===< %s >===", pattern);
  1463.             if (sent[0] != '\n')
  1464.                 putchar('\n');
  1465.             printf("%s\n", sent);
  1466.         }
  1467.     } while (more);
  1468.     if (resetno)
  1469.         lineno = 1;
  1470. }
  1471.  
  1472. char lastc = NULL;
  1473. int nl = 0;
  1474.  
  1475. getsent(s, lim, fp)    /* get sentence into s, return length */
  1476. char *s;
  1477. int lim;
  1478. FILE *fp;
  1479. {
  1480.     register int c;
  1481.  
  1482.     lineno += nl;
  1483.     nl = 0;
  1484.     while (--lim && (c = getc(fp)) != EOF && !is_eos(c, fp))
  1485.     {
  1486.         if (c == pgincr && isspace(lastc))
  1487.             pageno++;
  1488.         *s++ = c;
  1489.         lastc = c;
  1490.         if (c == '\n')
  1491.             nl++;
  1492.     }
  1493.     *s = NULL;
  1494.     lastc = c;
  1495.     if (c == '\n')
  1496.         nl++;
  1497.     if (c == EOF)
  1498.         return(0);
  1499.     return(1);
  1500. }
  1501.  
  1502. is_eos(c, fp)        /* determine if at end of sentence or record */
  1503. int c;
  1504. FILE *fp;
  1505. {
  1506.     int ch;
  1507.  
  1508.     if (recds)    /* searching records */
  1509.     {
  1510.         if (sepc == NULL)    /* RS is a blank line */
  1511.         {
  1512.             if (lastc == '\n' && c == '\n')
  1513.                 return(1);
  1514.         }
  1515.         else if (c == sepc)    /* RS is single character */
  1516.             return(1);
  1517.     }
  1518.     else if (isperiod(lastc))    /* searching sentences */
  1519.     {
  1520.         if (c == '\n')        /* period before eoln */
  1521.             return(1);
  1522.         ch = c;            /* test the next char */
  1523.         c = getc(fp);
  1524.         if (c == EOF)        /* do nothing if at EOF */
  1525.             ;
  1526.         if (isspace(ch) && isspace(c))    /* 2 spaces in row */
  1527.             return(1);
  1528.         if (ch == '"' && isspace(c))    /* quote and space */
  1529.             return(1);
  1530.         if (ch == ')' && isspace(c))    /* paren and space */
  1531.             return(1);
  1532.         ungetc(c, fp);
  1533.     }
  1534.     return(0);
  1535. }
  1536.  
  1537. index(s, pat)        /* return index of pat in s, -1 if no match */
  1538. char s[], pat[];
  1539. {
  1540.     register int i, j, k;
  1541.     int n, m;
  1542.  
  1543.     for (i = 0; s[i]; i++)
  1544.     {
  1545.         for (j = i, k = 0; pat[k]; j++, k++)
  1546.         {
  1547.             if (pat[k] == '\\')    /* escape char */
  1548.                 if (s[j] != pat[++k])
  1549.                     break;
  1550.             if (pat[k] == '$')    /* newline char */
  1551.                 if (s[j] == '\n')
  1552.                     continue;
  1553.             if (pat[k] == '_')    /* any character */
  1554.                 continue;
  1555.             if (pat[k] == '*')    /* wildcard */
  1556.             {
  1557.                 k++;
  1558.              forward:
  1559.                 while (s[j] && s[j] != pat[k])
  1560.                     j++;
  1561.                 for (m=j+1, n=k+1; s[m] && pat[n]; m++, n++) 
  1562.                     if (s[m] != pat[n]) {
  1563.                         j++;
  1564.                         goto forward;
  1565.                     }
  1566.             }
  1567.             if (s[j]=='\n' && pat[k]==' ')    /* ignore nl */
  1568.                 continue;
  1569.             if (s[j] != pat[k])
  1570.                 break;
  1571.         }
  1572.         if (pat[k] == NULL)
  1573.             return(i);
  1574.     }
  1575.     return(-1);
  1576. }
  1577. @@@ Fin de sfind.c
  1578. echo skel.c
  1579. cat >skel.c <<'@@@ Fin de skel.c'
  1580. # include <stdio.h>
  1581.  
  1582. main(argc, argv)    /* allow user to enter lines within skeleton */
  1583. int argc;
  1584. char *argv[];
  1585. {
  1586.     FILE *fopen(), *pfp, *fp;
  1587.     char s[BUFSIZ];
  1588.  
  1589.     if (argc != 2)
  1590.     {
  1591.         puts("Usage: skel filename\t\t(rev3.7)");
  1592.         exit(1);
  1593.     }
  1594.     if (access(argv[1], 0) == 0)
  1595.     {
  1596.         printf("File %s already exists.\n", argv[1]);
  1597.         exit(1);
  1598.     }
  1599.     if ((pfp = fopen("promptfile", "r")) == NULL)
  1600.     {
  1601.         puts("Required promptfile is missing.");
  1602.         exit(1);
  1603.     }
  1604.     if ((fp = fopen(argv[1], "w")) == NULL)
  1605.     {
  1606.         perror(argv[1]);
  1607.         exit(1);
  1608.     }
  1609.     while (fgets(s, BUFSIZ, pfp))
  1610.     {
  1611.         fputs(s, fp);
  1612.         fputs(s, stdout);
  1613.         do {
  1614.             putchar('\t');
  1615.             fgets(s, BUFSIZ, stdin);
  1616.             if (s[0] == '~' && (s[1] == 'e' || s[1] == 'v'))
  1617.             {
  1618.                 callex(fp, argv[1], s[1]);
  1619.                 puts("(continue)");
  1620.                 continue;
  1621.             }
  1622.             putc('\t', fp);
  1623.             if (s[0] == '.' && s[1] == '\n')
  1624.                 s[0] = '\003';        /* ctrl-c (etx) */
  1625.             fputs(s, fp);
  1626.         } while (s[0] != '\003' || s[1] != '\n');
  1627.     }
  1628. }
  1629.  
  1630. callex(fp, argv, e)    /* call ex editor to correct mistakes */
  1631. FILE *fp;
  1632. char *argv, e;
  1633. {
  1634.     FILE *tfp, *fopen();
  1635.     register int c;
  1636.     char command[80], *tempfile, *mktemp(), *sprintf();
  1637.  
  1638.     fp = freopen(argv, "r", fp);
  1639.     tempfile = "/tmp/SklXXXXX";
  1640.     mktemp(tempfile);
  1641.     tfp = fopen(tempfile, "w");
  1642.     while ((c = getc(fp)) != EOF)
  1643.         putc(c, tfp);
  1644.     if (ferror(tfp))
  1645.         perror(tempfile);
  1646.     fclose(tfp);
  1647.     if (e == 'e')
  1648.         sprintf(command, "ex %s", tempfile);
  1649.     if (e == 'v')
  1650.         sprintf(command, "vi +$ %s", tempfile);
  1651.     system(command);
  1652.     tfp = fopen(tempfile, "r");
  1653.     fp = freopen(argv, "w", fp);
  1654.     while ((c = getc(tfp)) != EOF)
  1655.         putc(c, fp);
  1656.     if (ferror(fp))
  1657.         perror(argv);
  1658.     fp = freopen(argv, "a", fp);
  1659.     unlink(tempfile);
  1660.     return;
  1661. }
  1662. @@@ Fin de skel.c
  1663. echo togrk.c
  1664. cat >togrk.c <<'@@@ Fin de togrk.c'
  1665. # include <stdio.h>                /* togrk.c (rev3.7) */
  1666.  
  1667. main(argc, argv)    /* transliterate Walton's Greek for nroff/troff */
  1668. int argc;
  1669. char *argv[];
  1670. {
  1671.     FILE *fopen(), *fp;
  1672.     int atbegin = 1;
  1673.  
  1674.     if (argc == 1)
  1675.     {
  1676.         tmacdef();
  1677.         togrk(stdin);
  1678.         exit(0);
  1679.     }
  1680.     while (--argc > 0)
  1681.     {
  1682.         if ((fp = fopen(*++argv, "r")) == NULL)
  1683.         {
  1684.             fflush(stdout);
  1685.             fprintf(stderr,
  1686.             "Togrk cannot access the file: %s\n", *argv);
  1687.             continue;
  1688.         }
  1689.         else  /* file opened successfully */
  1690.         {
  1691.             if (atbegin)
  1692.                 tmacdef();
  1693.             atbegin = 0;
  1694.             togrk(fp);
  1695.             fclose(fp);
  1696.         }
  1697.     }
  1698.     exit(0);
  1699. }
  1700.  
  1701. togrk(fp)        /* convert transliteration to troff strings */
  1702. FILE *fp;
  1703. {
  1704.     register int c;
  1705.  
  1706.     while ((c = getc(fp)) != EOF)
  1707.     {
  1708.         switch (c)
  1709.         {
  1710.         case 'a': printf("\\(*a"); break;    /* alpha */
  1711.         case 'A': printf("\\(*A"); break;
  1712.         case 'b': printf("\\(*b"); break;    /* beta */
  1713.         case 'B': printf("\\(*B"); break;
  1714.         case 'g': printf("\\(*g"); break;    /* gamma */
  1715.         case 'G': printf("\\(*G"); break;
  1716.         case 'd': printf("\\(*d"); break;    /* delta */
  1717.         case 'D': printf("\\(*D"); break;
  1718.         case 'e': printf("\\(*e"); break;    /* epsilon */
  1719.         case 'E': printf("\\(*E"); break;
  1720.         case 'z': printf("\\(*z"); break;    /* zeta */
  1721.         case 'Z': printf("\\(*Z"); break;
  1722.         case 'h': printf("\\(*y"); break;    /* eta(* */
  1723.         case 'H': printf("\\(*Y"); break;
  1724.         case 'q': printf("\\(*h"); break;    /* theta(*) */
  1725.         case 'Q': printf("\\(*H"); break;
  1726.         case 'i': printf("\\(*i"); break;    /* iota */
  1727.         case 'I': printf("\\(*I"); break;
  1728.             /* iota subscript(*) - j - J */
  1729.         case 'j': printf("\\(*i"); break;    /* iota subsript? */
  1730.         case 'J': printf("\\(*I"); break;
  1731.         case 'k': printf("\\(*k"); break;    /* kappa */
  1732.         case 'K': printf("\\(*K"); break;
  1733.         case 'l': printf("\\(*l"); break;    /* lambda */
  1734.         case 'L': printf("\\(*L"); break;
  1735.         case 'm': printf("\\(*m"); break;    /* mu */
  1736.         case 'M': printf("\\(*M"); break;
  1737.         case 'n': printf("\\(*n"); break;    /* nu */
  1738.         case 'N': printf("\\(*N"); break;
  1739.         case 'x': printf("\\(*c"); break;    /* xi */
  1740.         case 'X': printf("\\(*C"); break;
  1741.         case 'o': printf("\\(*o"); break;    /* omicron */
  1742.         case 'O': printf("\\(*O"); break;
  1743.         case 'p': printf("\\(*p"); break;    /* pi */
  1744.         case 'P': printf("\\(*P"); break;
  1745.         case 'r': printf("\\(*r"); break;    /* rho */
  1746.         case 'R': printf("\\(*R"); break;
  1747.         case 's': printf("\\(*s"); break;    /* sigma */
  1748.         case 'S': printf("\\(*S"); break;
  1749.         case 't': printf("\\(*t"); break;    /* tau */
  1750.         case 'T': printf("\\(*T"); break;
  1751.         case 'u': printf("\\(*u"); break;    /* upsilon */
  1752.         case 'U': printf("\\(*U"); break;
  1753.         case 'f': printf("\\(*f"); break;    /* phi(*) */
  1754.         case 'F': printf("\\(*F"); break;
  1755.         case 'c': printf("\\(*x"); break;    /* chi */
  1756.         case 'C': printf("\\(*X"); break;
  1757.         case 'y': printf("\\(*q"); break;    /* psi */
  1758.         case 'Y': printf("\\(*Q"); break;
  1759.         case 'w': printf("\\(*w"); break;    /* omega */
  1760.         case 'W': printf("\\(*W"); break;
  1761.             /* digamma - \fIf\fR - \fIF\fR (passed as is) */
  1762.         case '+': printf("\\s-2\\(pl\\s0"); break;
  1763.         case '-': printf("\\s-2\\(mi\\s0"); break;
  1764.             /* grave accent - change from \ to ` */
  1765.         case '`':printf("\\*`"); break;
  1766.         case '^': printf("\\*^"); break;
  1767.         case '/': printf("\\*'"); break;
  1768.             /* question - ; (why not ? ?) */
  1769.             /* diaeresis e.g. over alpha - \(*:a */
  1770.             /* must be changed to a\*: */
  1771.         default: putchar(c); break;
  1772.         }
  1773.     }
  1774. }
  1775.  
  1776. tmacdef()    /* define troff macros so macro package isn't needed */
  1777. {
  1778.     printf(".if n \\{\\\n");
  1779.     printf(".ds #H 0\n");
  1780.     printf(".ds #V 0.8m\n");
  1781.     printf(".ds #f +0.3m\n");
  1782.     printf(".ds #[ \\f1\n");
  1783.     printf(".ds #] \\fP\n");
  1784.     printf(".\\}\n");
  1785.     printf(".if t \\{\\\n");
  1786.     printf(".ds #H ((1u-(\\\\\\\\n(.fu%2u))*0.13m)\n");
  1787.     printf(".ds #V 0.6m\n");
  1788.     printf(".ds #f \"\n");
  1789.     printf(".ds #[ \\&\n");
  1790.     printf(".ds #] \\&\n");
  1791.     printf(".\\}\n");
  1792.     printf(".ds ' \\k_\\h'-(\\\\n(.wu*8/10-\\*(#H)'\\*(#[\\(aa\\h'|\\\\n_u'\\*(#]\n");
  1793.     printf(".ds ` \\k_\\h'-(\\\\n(.wu*8/10-\\*(#H)'\\*(#[\\(ga\\h'|\\\\n_u'\\*(#]\n");
  1794.     printf(".ds ^ \\k_\\h'-(\\\\n(.wu-\\*(#H)'\\*(#[^\\h'|\\\\n_u'\\*(#]\n");
  1795.     printf(".ds : \\k_\\h'-(\\\\n(.wu*8/10-\\*(#H+0.1m\\*(#f)'\\v'-\\*(#V'\\*(#[\\z.\\h'0.2m\\*(#f'.\\h'|\\\\n_u'\\v'\\*(#V'\\*(#]\n");
  1796.     printf(".rm #[ #] #H #V #f\n");
  1797. }
  1798. @@@ Fin de togrk.c
  1799. echo tolpr.c
  1800. cat >tolpr.c <<'@@@ Fin de tolpr.c'
  1801. # include <stdio.h>            /* tolpr.c (rev3.7) */
  1802. # include <ctype.h>
  1803.  
  1804. int spacing = 1;    /* spacing factor (2 = doublespace) */
  1805. char hdr[512];        /* page header for optional title */
  1806. int shift = 1;        /* toggle shift over one tab stop */
  1807.  
  1808. main(argc, argv)    /* send 8.5 inch wide output to lpr */
  1809. int argc;
  1810. char *argv[];
  1811. {
  1812.     FILE *fp, *fopen();
  1813.     int i, files = 0;
  1814.  
  1815.     for (i = 1; i < argc; i++)
  1816.     {
  1817.         if (*argv[i] == '-')
  1818.         {
  1819.             if (isdigit(argv[i][1]))
  1820.                 spacing = atoi(argv[i]+1);
  1821.             else if (argv[i][1] == 'h')
  1822.             {
  1823.                 strcpy(hdr, argv[++i]);
  1824.                 strcat(hdr, "  ");
  1825.             }
  1826.             else if (argv[i][1] == 's')
  1827.                 shift = 0;
  1828.             else {
  1829.                 fprintf(stderr, "Invalid tolpr flag: %s\n",
  1830.                     argv[i]);
  1831.                 exit(1);
  1832.             }
  1833.         }
  1834.         else if ((fp = fopen(argv[i], "r")) != NULL)
  1835.         {
  1836.             files = 1;
  1837.             tolpr(fp);
  1838.             fclose(fp);
  1839.         }
  1840.         else  /* cannot open file */
  1841.         {
  1842.             fprintf(stderr,
  1843.             "Tolpr cannot access the file: %s\n", argv[i]);
  1844.             exit(1);
  1845.         }
  1846.     }
  1847.     if (!files)
  1848.         tolpr(stdin);
  1849.     exit(0);
  1850. }
  1851.  
  1852. tolpr(fp)        /* insert tab at BOL, remarry widows */
  1853. FILE *fp;
  1854. {
  1855.     char s[BUFSIZ], first = 1, paginate = 0, empty;
  1856.     static int pageno = 1;
  1857.     register int lno;
  1858.     int i;
  1859.  
  1860.     for (lno = 1; fgets(s, BUFSIZ, fp); lno++)
  1861.     {
  1862.         if (first)            /* determine page scheme */
  1863.         {
  1864.             if (s[0] != '\n')
  1865.                 paginate = 1;
  1866.             first = 0;
  1867.         }
  1868.         if (paginate && lno == 1)    /* provide 3 blank lines */
  1869.         {
  1870.             printf("\n\n\n");
  1871.             if (shift)
  1872.                 putchar('\t');
  1873.             printf("%sPage %d\n\n", hdr, pageno++);
  1874.             lno = 6;
  1875.         }
  1876.         else if (paginate && lno == 63)     /* save widow, new page */
  1877.         {
  1878.             if (empty)
  1879.             {
  1880.                 printf("\n\n\n\n\n\n\n");
  1881.                 if (shift)
  1882.                     putchar('\t');
  1883.                 printf("%sPage %d\n\n", hdr,pageno++);
  1884.                 lno = 6;
  1885.             }
  1886.         }
  1887.         else if (paginate && lno >= 64)     /* break to new page */
  1888.         {
  1889.             breakpg:
  1890.                 printf("\n\n\n\n\n\n");
  1891.                 if (shift)
  1892.                     putchar('\t');
  1893.                 printf("%sPage %d\n\n", hdr,pageno++);
  1894.                 lno = 6;
  1895.         }
  1896.         else if (lno >= 66)        /* reset page cycle */
  1897.             lno = 1;
  1898.  
  1899.         for (i = 1; i < spacing; i++)    /* optional linespacing */
  1900.         {
  1901.             putchar('\n');
  1902.             lno++;
  1903.             if (lno >= 64)
  1904.                 goto breakpg;
  1905.         }
  1906.         if (s[0] != '\n' && shift)    /* don't pad empty lines */
  1907.         {
  1908.             putchar('\t');
  1909.             empty = 0;        /* remember empty lines */
  1910.         }
  1911.         else
  1912.             empty = 1;
  1913.  
  1914.         fputs(s, stdout);
  1915.     }
  1916.     while (lno++ <= 66)            /* force out page */
  1917.         putchar('\n');
  1918. }
  1919. @@@ Fin de tolpr.c
  1920. echo tosel.c
  1921. cat >tosel.c <<'@@@ Fin de tosel.c'
  1922. # include <stdio.h>            /* tosel.c (rev3.7) */
  1923.  
  1924. main(argc, argv)    /* convert English for IBM terminal */
  1925. int argc;
  1926. char *argv[];
  1927. {
  1928.     FILE *fp, *fopen();
  1929.  
  1930.     if (argc == 1)
  1931.         convert(stdin);
  1932.     else
  1933.     {
  1934.         while (--argc > 0)
  1935.             if ((fp = fopen(*++argv, "r")) == NULL)
  1936.             {
  1937.                 fprintf(stderr,
  1938.                 "Tosel cannot access the file: %s\n",
  1939.                 *argv);
  1940.                 continue;
  1941.             }
  1942.             else
  1943.             {
  1944.                 convert(fp);
  1945.                 fclose(fp);
  1946.             }
  1947.         exit(0);
  1948.     }
  1949. }
  1950.  
  1951. convert(fp)        /* Unix ASCII to type-ball EBCDIC */
  1952. FILE *fp;
  1953. {
  1954.     char c;
  1955.  
  1956.     while ((c = getc(fp)) != EOF)
  1957.         switch (c)
  1958.         {
  1959.         case 'a': putchar(c = 'p'); break;
  1960.         case 'b': putchar(c = ','); break;
  1961.         case 'c': putchar(c = 'x'); break;
  1962.         case 'd': putchar(c = 'v'); break;
  1963.         case 'e': putchar(c = 'u'); break;
  1964.         case 'f': putchar(c = 'c'); break;
  1965.         case 'g': putchar(c = 'a'); break;
  1966.         case 'h': putchar(c = 'z'); break;
  1967.         case 'i': putchar(c = 'o'); break;
  1968.         case 'j': putchar(c = '&'); break;
  1969.         case 'k': putchar(c = 'w'); break;
  1970.         case 'l': putchar(c = 'y'); break;
  1971.         case 'm': putchar(c = 'j'); break;
  1972.         case 'n': putchar(c = 's'); break;
  1973.         case 'o': putchar(c = 'q'); break;
  1974.         case 'p': putchar(c = 'd'); break;
  1975.         case 'q': putchar(c = 'f'); break;
  1976.         case 'r': putchar(c = 'n'); break;
  1977.         case 's': putchar(c = 'r'); break;
  1978.         case 't': putchar(c = '@'); break;
  1979.         case 'u': putchar(c = 't'); break;
  1980.         case 'v': putchar(c = 'l'); break;
  1981.         case 'w': putchar(c = '$'); break;
  1982.         case 'x': putchar(c = '/'); break;
  1983.         case 'y': putchar(c = 'i'); break;
  1984.         case 'z': putchar(c = '0'); break;
  1985.         case '.': putchar(c = 'k'); break;
  1986.         case ',': putchar(c = 'g'); break;
  1987.         case ';': putchar(c = 'e'); break;
  1988.         case ':': putchar(c = 'E'); break;
  1989.         case '-': putchar(c = '.'); break;
  1990.         case '!': putchar(c = '='); break;    
  1991.         case '?': putchar(c = 'H'); break;
  1992.         case '/': putchar(c = 'h'); break;
  1993.         case '"': putchar(c = 'M'); break;
  1994.         case '\'': putchar(c = 'm'); break;
  1995.         case '(': putchar(c = '"'); break;
  1996.         case ')': putchar(c = '('); break;
  1997.         case '_': putchar(c = '~'); break;
  1998.         case '%': putchar(c = ':'); break;
  1999.         case '&': putchar(c = '%'); break;
  2000.         case '$': putchar(c = '*'); break;
  2001.         case '=': putchar(c = 'b'); break;
  2002.         case '+': putchar(c = 'B'); break;
  2003.         case '*': putchar(c = '>'); break;
  2004.         case '@': putchar(c = '<'); break;
  2005.         case '#': putchar(c = ';'); break;
  2006.         case '0': putchar(c = '9'); break;
  2007.         case '4': putchar(c = '8'); break;
  2008.         case '5': putchar(c = '4'); break;
  2009.         case '7': putchar(c = '5'); break;
  2010.         case '8': putchar(c = '7'); break;
  2011.         case '9': putchar(c = '#'); break;
  2012.         case 'A': putchar(c = 'P'); break;
  2013.         case 'B': putchar(c = '|'); break;
  2014.         case 'C': putchar(c = 'X'); break;
  2015.         case 'D': putchar(c = 'V'); break;
  2016.         case 'E': putchar(c = 'U'); break;
  2017.         case 'F': putchar(c = 'C'); break;
  2018.         case 'G': putchar(c = 'A'); break;
  2019.         case 'H': putchar(c = 'Z'); break;
  2020.         case 'I': putchar(c = 'O'); break;
  2021.         case 'J': putchar(c = '+'); break;
  2022.         case 'K': putchar(c = 'W'); break;
  2023.         case 'L': putchar(c = 'Y'); break;
  2024.         case 'M': putchar(c = 'J'); break;
  2025.         case 'N': putchar(c = 'S'); break;
  2026.         case 'O': putchar(c = 'Q'); break;
  2027.         case 'P': putchar(c = 'D'); break;
  2028.         case 'Q': putchar(c = 'F'); break;
  2029.         case 'R': putchar(c = 'N'); break;
  2030.         case 'S': putchar(c = 'R'); break;
  2031.         case 'T': putchar(c = '\\'); break;
  2032.         case 'U': putchar(c = 'T'); break;
  2033.         case 'V': putchar(c = 'L'); break;
  2034.         case 'W': putchar(c = '!'); break;
  2035.         case 'X': putchar(c = '?'); break;
  2036.         case 'Y': putchar(c = 'I'); break;
  2037.         case 'Z': putchar(c = ')'); break;
  2038.         case '[': putchar(c = '"'); break; /* prints ( */
  2039.         case '{': putchar(c = '"'); break;
  2040.         case ']': putchar(c = '('); break; /* prints ) */
  2041.         case '}': putchar(c = '('); break;
  2042.         case '`': putchar(c = 'm'); break;
  2043.         default: putchar(c); break;
  2044.         }
  2045. }
  2046. @@@ Fin de tosel.c
  2047. echo tprep.c
  2048. cat >tprep.c <<'@@@ Fin de tprep.c'
  2049. # include <stdio.h>            /* tprep.c (rev3.7) */
  2050. # include <ctype.h>
  2051. # include <signal.h>
  2052.  
  2053. usage()        /* print usage and synopsis of options */
  2054. {
  2055.     puts("Usage: tprep [-y -tpu] filename(s)\t\t(rev3.7)");
  2056.     puts("-y: say yes and suppress interactive prompting");
  2057.     puts("-t: trim lines, removing trailing blanks and tabs");
  2058.     puts("-p: pad, inserting blank at beginning of each line");
  2059.     puts("-u: unpad, deleting blank at beginning of each line");
  2060.     puts("Tprep rewrites files back on top of themselves.");
  2061.     exit(1);
  2062. }
  2063.  
  2064. char *tempfile;        /* storage file for modified text */
  2065. int askflag = 1;    /* ask user's permission (default on) */
  2066. int tflag = 0;        /* toggle option for trimming */
  2067. int pflag = 0;        /* toggle option for padding */
  2068. int uflag = 0;        /* toggle option for unpadding */
  2069.  
  2070. main(argc, argv)    /* insert or delete leading blanks */
  2071. int argc;
  2072. char *argv[];
  2073. {
  2074.     FILE *fp, *fopen();
  2075.     char *s, *mktemp();
  2076.     int i = 1;
  2077.  
  2078.     if (argc == 1)
  2079.         usage();
  2080.  
  2081.     for (i = 1; i < argc && *argv[i] == '-'; i++)
  2082.         for (s = argv[i]+1; *s != NULL; s++)
  2083.             if (*s == 'y')
  2084.                 askflag = 0;
  2085.             else if (*s == 't')
  2086.                 tflag = 1;
  2087.             else if (*s == 'p')
  2088.                 pflag = 1;
  2089.             else if (*s == 'u')
  2090.                 uflag = 1;
  2091.             else
  2092.             {
  2093.                 fprintf(stderr,
  2094.                 "Illegal tprep option: -%c\n", *s);
  2095.                 exit(1);
  2096.             }
  2097.     if (tflag + pflag + uflag > 1)
  2098.     {
  2099.         fprintf(stderr, "Using incompatible tprep options\n");
  2100.         exit(1);
  2101.     }
  2102.     if (askflag)
  2103.         permission();
  2104.     if (!tflag && !pflag && !uflag)
  2105.         options();
  2106.  
  2107.     signal(SIGINT, SIG_IGN);
  2108.     tempfile = "/tmp/PrepXXXXX";
  2109.     mktemp(tempfile);
  2110.  
  2111.     for (; i < argc; i++)
  2112.     {
  2113.         if ((fp = fopen(argv[i], "r")) != NULL)
  2114.         {
  2115.             fprintf(stderr,
  2116.             "Rewriting the file: %s\n", argv[i]);
  2117.             if (tflag)
  2118.                 trim(fp);
  2119.             if (pflag)
  2120.                 pad(fp);
  2121.             if (uflag)
  2122.                 unpad(fp);
  2123.             if (freopen(argv[i], "w", fp) == NULL)
  2124.             {
  2125.                 fprintf(stderr,
  2126.                 "%s: Write permission denied\n", argv[i]);
  2127.                 unlink(tempfile);
  2128.                 exit(1);
  2129.             }
  2130.             overwrite(fp);
  2131.             fclose(fp);
  2132.         }
  2133.         else  /* attempt to open file failed */
  2134.         {
  2135.             fprintf(stderr,
  2136.             "Tprep cannot access the file: %s\n", argv[i]);
  2137.             unlink(tempfile);
  2138.             exit(1);
  2139.         }
  2140.     }
  2141.     unlink(tempfile);
  2142.     exit(0);
  2143. }
  2144.  
  2145. permission()        /* make sure user agrees to rewrite files */
  2146. {
  2147.     FILE *term, *fopen();
  2148.     char ans[512];
  2149.  
  2150.     term = fopen("/dev/tty", "r");
  2151.     fprintf(stderr, "Do you know tprep rewrites files in command line?\n");
  2152.     fprintf(stderr, "Type \"y\" if this is OK with you: ");
  2153.     fgets(ans, 512, term);
  2154.     if (*ans == 'y' || *ans == 'Y')
  2155.         putc('\n', stderr);
  2156.     else
  2157.     {
  2158.         fprintf(stderr, "If it makes you feel safer, ");
  2159.         fprintf(stderr, "you could copy files to /usr/tmp.\n");
  2160.         exit(1);
  2161.     }
  2162.     return;
  2163. }
  2164.  
  2165. options()        /* ask user whether to trim, pad or unpad */
  2166. {
  2167.     FILE *term, *fopen();
  2168.     char ans[512];
  2169.     int done = 0;
  2170.  
  2171.     term = fopen("/dev/tty", "r");
  2172.     while (!done)
  2173.     {
  2174.         fprintf(stderr, "Do you want to trim, pad, or unpad? ");
  2175.         fgets(ans, 512, term);
  2176.         if (*ans == 't' || *ans == 'T')
  2177.         {
  2178.             tflag = 1;
  2179.             done = 1;
  2180.         }
  2181.         else if (*ans == 'p' || *ans == 'P')
  2182.         {
  2183.             pflag = 1;
  2184.             done = 1;
  2185.         }
  2186.         else if (*ans == 'u' || *ans == 'U')
  2187.         {
  2188.             uflag = 1;
  2189.             done = 1;
  2190.         }
  2191.         else if (*ans == 'q' || *ans == 'Q')
  2192.             exit(1);
  2193.         else
  2194.             fprintf(stderr,
  2195.             "\tJust answer \"t\", \"p\" or \"u\" (\"q\" exits).\n");
  2196.     }
  2197.     putc('\n', stderr);
  2198.     return;
  2199. }
  2200.  
  2201. trim(fp)        /* trim off blanks and send to tempfile */
  2202. FILE *fp;
  2203. {
  2204.     FILE *tfp, *fopen();
  2205.     char line[512];
  2206.     long count = 0;
  2207.     int n;
  2208.  
  2209.     tfp = fopen(tempfile, "w");
  2210.     while (fgets(line, 512, fp))
  2211.     {
  2212.         n = strlen(line);
  2213.         while (--n >= 0)
  2214.         {
  2215.             if (!isspace(line[n]))
  2216.                 break;
  2217.             count++;
  2218.         }
  2219.         line[++n] = '\n';
  2220.         line[++n] = NULL;
  2221.         fputs(line, tfp);
  2222.         count--;        /* uncount newline */
  2223.     }
  2224.     fclose(tfp);
  2225.     fprintf(stderr, "\t%ld blanks and tabs removed\n", count);
  2226. }
  2227.  
  2228. pad(fp)            /* pad line with blank and send to tempfile */
  2229. FILE *fp;
  2230. {
  2231.     FILE *tfp, *fopen();
  2232.     char line[512];
  2233.     int count = 0;
  2234.  
  2235.     tfp = fopen(tempfile, "w");
  2236.     while (fgets(line, 512, fp))
  2237.     {
  2238.         fprintf(tfp, " %s", line);
  2239.         count++;
  2240.     }
  2241.     fclose(tfp);
  2242.     fprintf(stderr, "\t%d lines padded\n", count);
  2243. }
  2244.  
  2245. unpad(fp)        /* strip leading blank and send to tempfile */
  2246. FILE *fp;
  2247. {
  2248.     FILE *tfp, *fopen();
  2249.     char line[512], copy[512];
  2250.     int i, j, count = 0;
  2251.  
  2252.     tfp = fopen(tempfile, "w");
  2253.     while (fgets(line, 512, fp))
  2254.     {
  2255.         i = 0;
  2256.         if (line[i] == ' ')
  2257.         {
  2258.             i++;
  2259.             count++;
  2260.         }
  2261.         for (j = 0; line[i] != NULL; i++, j++)
  2262.             copy[j] = line[i];
  2263.         copy[j] = NULL;
  2264.         fputs(copy, tfp);
  2265.     }
  2266.     fclose(tfp);
  2267.     fprintf(stderr, "\t%d lines unpadded\n", count);
  2268. }
  2269.  
  2270. overwrite(fp)        /* write tempfile back over original file */
  2271. FILE *fp;
  2272. {
  2273.     FILE *tfp, *fopen();
  2274.     char line[512];
  2275.  
  2276.     tfp = fopen(tempfile, "r");
  2277.     while (fgets(line, 512, tfp))
  2278.         fputs(line, fp);
  2279.     fclose(tfp);
  2280. }
  2281. @@@ Fin de tprep.c
  2282. echo troffmt.c
  2283. cat >troffmt.c <<'@@@ Fin de troffmt.c'
  2284. # include <stdio.h>            /* troffmt.c (rev3.7) */
  2285. # include <ctype.h>
  2286. # include <signal.h>
  2287.  
  2288. char *tempfile;        /* to store contexts while counting */
  2289. int nocnt = 0;        /* toggle for not counting keyword */
  2290. int nokwd = 0;        /* toggle for suppressing keyword */
  2291. int nomacs = 0;        /* toggle for not supplying macros */
  2292.  
  2293. usage()            /* print proper usage and exit */
  2294. {
  2295.     puts("Usage: troffmt [-ckm] [filename(s)] [-]\t\t(rev3.7)");
  2296.     puts("-c: suppress counting of keyword frequency");
  2297.     puts("-k: entirely suppress printing of keyword");
  2298.     puts("-m: do not supply concordance macros automatically");
  2299.     puts("- : read standard input instead of files");
  2300.     exit(1);
  2301. }
  2302.  
  2303. main(argc, argv)    /* format concordance for sending to troff */
  2304. int argc;
  2305. char *argv[];
  2306. {
  2307.     FILE *fopen(), *fp;
  2308.     int i, j, onintr();
  2309.     char *mktemp();
  2310.  
  2311.     if (signal(SIGINT, SIG_IGN) != SIG_IGN)
  2312.         signal(SIGINT, onintr);
  2313.  
  2314.     tempfile = "/tmp/FmtXXXXX";
  2315.     mktemp(tempfile);
  2316.  
  2317.     for (i = 1; *argv[i] == '-'; i++)
  2318.     {
  2319.         for (j = 1; argv[i][j] != NULL; j++)
  2320.         {
  2321.             if (argv[i][j] == 'c')
  2322.                 nocnt = 1;
  2323.             else if (argv[i][j] == 'k')
  2324.                 nokwd = 1;
  2325.             else if (argv[i][j] == 'm')
  2326.                 nomacs = 1;
  2327.             else  /* bad option */
  2328.             {
  2329.                 fprintf(stderr,
  2330.                 "Illegal troffmt flag: -%c\n", argv[i][j]);
  2331.                 usage();
  2332.             }
  2333.         }
  2334.     }
  2335.     if (!nomacs)
  2336.         pr_macs();
  2337.     if (i == argc)
  2338.     {
  2339.         if (nokwd)
  2340.             rmkwds(stdin);
  2341.         else if (nocnt)
  2342.             ffmt(stdin);
  2343.         else
  2344.             format(stdin);
  2345.     }
  2346.     for (; i < argc; i++)
  2347.     {
  2348.         if (argv[i][0] == '-' && argv[i][1] == NULL)
  2349.         {
  2350.             if (nokwd)
  2351.                 rmkwds(stdin);
  2352.             else if (nocnt)
  2353.                 ffmt(stdin);
  2354.             else
  2355.                 format(stdin);
  2356.         }
  2357.         if ((fp = fopen(argv[i], "r")) != NULL)
  2358.         {
  2359.             if (nokwd)
  2360.                 rmkwds(fp);
  2361.             else if (nocnt)
  2362.                 ffmt(fp);
  2363.             else
  2364.                 format(fp);
  2365.             fclose(fp);
  2366.         }
  2367.         else  /* attempt to open file failed */
  2368.         {
  2369.             fprintf(stderr,
  2370.             "Trfmt cannot access the file: %s\n", argv[i]);
  2371.             continue;
  2372.         }
  2373.     }
  2374.     unlink(tempfile);
  2375.     exit(0);
  2376. }
  2377.  
  2378. pr_macs()        /* supply concordance macros automatically */
  2379. {
  2380.     printf(".de KW\n.ne 2.1\n.sp 0.3\n.ta 20n\n.ft B\n..\n");
  2381.     printf(".de CX\n.sp 0.1\n.ta 3.6iR 3.75i\n.ft R\n..\n");
  2382.     printf(".de HD\n.tl '--''--'\n'sp 2\n.tl ''- %% -''\n'sp 2\n..\n");
  2383.     printf(".de FO\n'bp\n..\n.wh 0 HD\n.wh -5 FO\n");
  2384.     printf(".so /usr/lib/me/chars.me\n");        /* -me accent marks */
  2385.     printf(".nf\n.po 0.5i\n.ll 7.0i\n.lt 7.0i\n");
  2386. }
  2387.  
  2388. format(fp)          /* print keyword and count only if different */
  2389. FILE *fp;
  2390. {
  2391.     FILE *fopen(), *tf;
  2392.     char s[BUFSIZ], okw[BUFSIZ/2], nkw[BUFSIZ/2], cntxt[BUFSIZ];
  2393.     char *sp, *kwp, *cxp, *strcpy();
  2394.     int kwfreq = 0;
  2395.  
  2396.     strcpy(okw,"~~~~~");    /* make sure 1st keyword is printed */
  2397.     tf = NULL;        /* to prevent core dump with no input */
  2398.     while (fgets(s, BUFSIZ, fp))
  2399.     {
  2400.         for (sp = s, kwp = nkw; *sp != ' ' && *sp != '|'; sp++, kwp++)
  2401.         {
  2402.             if (*sp == '\b')    /* interpolate troff string */
  2403.             {
  2404.                 *kwp = '\\';
  2405.                 *++kwp = '*';
  2406.             } else
  2407.                 *kwp = *sp;
  2408.         }
  2409.         *kwp = NULL;
  2410.         for (; *sp && *sp != '|'; sp++)
  2411.             ;
  2412.         for (++sp, cxp = cntxt; *sp && *sp != '\n'; sp++, cxp++)
  2413.         {
  2414.             if (*sp == '|')
  2415.                 *cxp = '\t';
  2416.             else if (*sp == '\b')    /* interpolate troff string */
  2417.             {
  2418.                 *cxp = '\\';
  2419.                 *++cxp = '*';
  2420.             } else
  2421.                 *cxp = *sp;
  2422.         }
  2423.         *cxp = '\n';
  2424.         *++cxp = NULL;
  2425.  
  2426.         if (strcmp(nkw, okw) != 0)  /* kwds different */
  2427.         {
  2428.             if (kwfreq != 0)
  2429.                 prtmpfile(tf, kwfreq);
  2430.             printf(".KW\n%s\t", nkw);
  2431.             tf = fopen(tempfile, "w");
  2432.             fputs(cntxt, tf);
  2433.             kwfreq = 1;
  2434.         }
  2435.         else  /* if keywords are the same */
  2436.         {
  2437.             fputs(cntxt, tf);
  2438.             kwfreq++;
  2439.         }
  2440.         strcpy(okw, nkw);
  2441.     }
  2442.     prtmpfile(tf, kwfreq);
  2443. }
  2444.  
  2445. prtmpfile(tf, kwfreq)        /* print frequency and contexts */
  2446. FILE *tf;
  2447. int kwfreq;
  2448. {
  2449.     char save[BUFSIZ];
  2450.  
  2451.     if (tf == NULL)        /* exit without core dump */
  2452.         exit(1);
  2453.  
  2454.     printf("(%d)\n.CX\n", kwfreq);
  2455.     fclose(tf);
  2456.     tf = fopen(tempfile, "r");
  2457.     while (fgets(save, BUFSIZ, tf))
  2458.         printf(" %s", save);
  2459.     fclose(tf);
  2460. }
  2461.  
  2462. int onintr()        /* remove tempfile in case of interrupt */
  2463. {
  2464.     fprintf(stderr, "\nInterrupt\n");
  2465.     unlink(tempfile);
  2466.     exit(1);
  2467. }
  2468.  
  2469. ffmt(fp)          /* fast format routine, no keyword counting */
  2470. FILE *fp;
  2471. {
  2472.     char s[BUFSIZ], okw[BUFSIZ/2], nkw[BUFSIZ/2], cntxt[BUFSIZ];
  2473.     char *sp, *kwp, *cxp;
  2474.  
  2475.     strcpy(okw,"~~~~~");    /* make sure 1st keyword is printed */
  2476.  
  2477.     while (fgets(s, BUFSIZ, fp))
  2478.     { 
  2479.         for (sp = s, kwp = nkw; *sp && *sp != ' '; sp++, kwp++)
  2480.         {
  2481.             if (*sp == '\b')    /* interpolate troff string */
  2482.             {
  2483.                 *kwp = '\\';
  2484.                 *++kwp = '*';
  2485.             } else
  2486.                 *kwp = *sp;
  2487.         }
  2488.         *kwp = NULL;
  2489.         for (; *sp && *sp != '|'; sp++)
  2490.             ;
  2491.         for (++sp, cxp = cntxt; *sp && *sp != '\n'; sp++, cxp++)
  2492.         {
  2493.             if (*sp == '|')
  2494.                 *cxp = '\t';
  2495.             else if (*sp == '\b')    /* interpolate troff string */
  2496.             {
  2497.                 *cxp = '\\';
  2498.                 *++cxp = '*';
  2499.             } else
  2500.                 *cxp = *sp;
  2501.         }
  2502.         *cxp = '\n';
  2503.         *++cxp = NULL;
  2504.  
  2505.         if (strcmp(nkw, okw) != 0)  /* kwds different */
  2506.             printf(".KW\n%s\n.CX\n %s", nkw, cntxt);
  2507.         else  /* if keywords are the same */
  2508.             printf(" %s", cntxt);
  2509.         strcpy(okw, nkw);
  2510.     }
  2511. }
  2512.  
  2513. rmkwds(fp)        /* completely suppress printing of keyword */
  2514. FILE *fp;
  2515. {
  2516.     char s[BUFSIZ], *sp;
  2517.  
  2518.     while (fgets(s, BUFSIZ, fp))
  2519.     {
  2520.         for (sp = s; *sp && *sp != '|'; sp++)
  2521.             ;
  2522.         for (; *sp; sp++)
  2523.         {
  2524.             if (*sp == '|')
  2525.                 putchar(' ');
  2526.             else
  2527.                 putchar(*sp);
  2528.         }
  2529.     }
  2530. }
  2531. @@@ Fin de troffmt.c
  2532. echo wdlen.c
  2533. cat >wdlen.c <<'@@@ Fin de wdlen.c'
  2534. # include <stdio.h>            /* wdlen.c (rev3.7) */
  2535. # include <ctype.h>
  2536.  
  2537. char punctuation[512] = ",.;:-?!\"()[]{}" ;
  2538.  
  2539. int wdlngs[21];        /* array with counts of word lengths */
  2540. int longlpr = 0;    /* toggle option for long histogram */
  2541.  
  2542. main(argc, argv)    /* tabulate word length frequency */
  2543. int argc;
  2544. char *argv[];
  2545. {
  2546.     FILE *fopen(), *fp;
  2547.     int i;
  2548.  
  2549.     if (argc == 1)
  2550.     {
  2551.         puts("Usage: wdlen [-l -dPfile -] filename(s)\t\t(rev3.7)");
  2552.         puts("-l: print long histogram suitable for lineprinter");
  2553.         puts("-d: define punctuation set according to Pfile");
  2554.         puts("- : read standard input instead of files");
  2555.         exit(1);
  2556.     }
  2557.     initialize();
  2558.     for (i = 1; i < argc; i++)
  2559.     {
  2560.         if (*argv[i] == '-')
  2561.             getflag(argv[i]);
  2562.         else if ((fp = fopen(argv[i], "r")) != NULL)
  2563.         {
  2564.             wdlen(fp);
  2565.             fclose(fp);
  2566.         }
  2567.         else
  2568.         {
  2569.             fprintf(stderr,
  2570.             "Wdlen cannot access the file: %s\n", argv[i]);
  2571.             exit(1);
  2572.         }
  2573.     }
  2574.     pr_results();
  2575.     exit(0);
  2576. }
  2577.  
  2578. initialize()        /* set word length array to all zeros */
  2579. {
  2580.     int i;
  2581.  
  2582.     for (i = 0; i < 21; i++)
  2583.         wdlngs[i] = 0;
  2584. }
  2585.  
  2586. getflag(f)        /* parses command line to set options */
  2587. char *f;
  2588. {
  2589.     char *pfile;
  2590.  
  2591.     f++;
  2592.     switch(*f++)
  2593.     {
  2594.         case 'l':
  2595.             longlpr = 1;
  2596.             break;
  2597.         case 'd':
  2598.             pfile = f;
  2599.             getpunct(pfile);
  2600.             break;
  2601.         case NULL:
  2602.             wdlen(stdin);
  2603.             break;
  2604.         default:
  2605.             fprintf(stderr,
  2606.             "Invalid wdlen flag: -%s\n", --f);
  2607.             exit(1);
  2608.             break;
  2609.     }
  2610. }
  2611.  
  2612. getpunct(pfile)        /* read user's punctuation from pfile */
  2613. char *pfile;
  2614. {
  2615.     FILE *pfp, *fopen();
  2616.     char s[512], *strcpy();
  2617.  
  2618.     if ((pfp = fopen(pfile, "r")) == NULL)
  2619.     {
  2620.         fprintf(stderr,
  2621.         "Wdlen cannot access Punctfile: %s\n", pfile);
  2622.         exit(1);
  2623.     }
  2624.     else
  2625.         while (fgets(s, 512, pfp))
  2626.             strcpy(punctuation, s);
  2627. }
  2628.  
  2629. wdlen(fp)        /* tabulate array of word lengths */
  2630. FILE *fp;
  2631. {
  2632.     int wlen;
  2633.  
  2634.     while (wlen = getword(fp))
  2635.         if (wlen < 21)
  2636.             ++wdlngs[wlen];
  2637. }
  2638.  
  2639. getword(fp)        /* move through text, counting word lengths */
  2640. FILE *fp;
  2641. {
  2642.     int wlen = 1;
  2643.     register int c;
  2644.  
  2645.     while ((c = getc(fp)) != EOF && isskip(c))
  2646.         ;
  2647.     if (c == EOF)
  2648.         return(NULL);
  2649.     while ((c = getc(fp)) != EOF && !isskip(c))
  2650.         wlen++;
  2651.     return(wlen);
  2652. }
  2653.  
  2654. isskip(c)        /* function to evaluate punctuation */
  2655. char c;
  2656. {
  2657.     char *ptr;
  2658.  
  2659.     if (isspace(c))
  2660.         return(1);
  2661.     for (ptr = punctuation; *ptr != c && *ptr != NULL; ptr++)
  2662.         ;
  2663.     if (*ptr == NULL)
  2664.         return(0);
  2665.     else
  2666.         return(1);
  2667. }
  2668.  
  2669. pr_results()        /* print out table of word lengths */
  2670. {
  2671.     int max, incr, i, j;
  2672.  
  2673.     max = 0;
  2674.     for (i = 1; i < 21; i++)
  2675.         if (wdlngs[i] > max)
  2676.             max = wdlngs[i];
  2677.     if (longlpr)
  2678.         incr = max/100;
  2679.     else
  2680.         incr = max/60;
  2681.     if (incr == 0)
  2682.         incr = 1;
  2683.     printf("Wdlen: Count:\n");
  2684.     for (i = 1; i < 21; i++)
  2685.     {
  2686.         printf("   %2d  ", i);
  2687.         printf("%5d  ", wdlngs[i]);
  2688.         for (j = 0; j < wdlngs[i]; j = j + incr)
  2689.             putchar('-');
  2690.         putchar('\n');
  2691.     }
  2692. }
  2693. @@@ Fin de wdlen.c
  2694. echo wheel.c
  2695. cat >wheel.c <<'@@@ Fin de wheel.c'
  2696. # include <stdio.h>            /* wheel.c (rev3.7) */
  2697. # include <ctype.h>
  2698.  
  2699. int nomap = 0;        /* toggle to not map upper to lower case */
  2700. int notch = 2;        /* number of words rolling around wheel */
  2701.  
  2702. main(argc, argv)    /* print overlapping word clusters in text */
  2703. int argc;
  2704. char *argv[];
  2705. {
  2706.     FILE *fp, *fopen();
  2707.     int i;
  2708.  
  2709.     if (argc == 1)
  2710.     {
  2711.         puts("Usage: wheel [+n -m -dF -] filename(s)\t\t(rev3.7)");
  2712.         puts("+n: print clusters of n words (default 2)");
  2713.         puts("-m: do not map upper case to lower case");
  2714.         puts("-d: define punctuation set according to file F");
  2715.         puts("- : read standard input instead of files");
  2716.         exit(1);
  2717.     }
  2718.     for (i = 1; i < argc; i++)
  2719.     {
  2720.         if (*argv[i] == '-')
  2721.             getflag(argv[i]);
  2722.         else if (*argv[i] == '+')
  2723.         {
  2724.             notch = atoi(argv[i]+1);
  2725.             if (notch > 20)
  2726.             {
  2727.                 fprintf(stderr,
  2728.                 "Clusters must be smaller than 20.\n");
  2729.                 exit(1);
  2730.             }
  2731.         }
  2732.         else if ((fp = fopen(argv[i], "r")) != NULL)
  2733.         {
  2734.             roll(fp);
  2735.             fclose(fp);
  2736.         }
  2737.         else  /* cannot open file */
  2738.         {
  2739.             fprintf(stderr,
  2740.             "Wheel cannot access the file: %s\n", argv[i]);
  2741.             continue;
  2742.         }
  2743.     }
  2744.     exit(0);
  2745. }
  2746.  
  2747. getflag(f)        /* parses command line to set options */
  2748. char *f;
  2749. {
  2750.     char *pfile;
  2751.  
  2752.     f++;
  2753.     switch(*f++)
  2754.     {
  2755.         case 'm':
  2756.             nomap = 1;
  2757.             break;
  2758.         case 'd':
  2759.             pfile = f;
  2760.             getpunct(pfile);
  2761.             break;
  2762.         case NULL:
  2763.             roll(stdin);
  2764.             break;
  2765.         default:
  2766.             fprintf(stderr,
  2767.             "Invalid wheel flag: -%s\n", --f);
  2768.             exit(1);
  2769.             break;
  2770.     }
  2771. }
  2772.  
  2773. char punctuation[BUFSIZ] = ",.;:-?!\"()[]{}" ;
  2774.  
  2775. getpunct(pfile)        /* read user's punctuation from pfile */
  2776. char *pfile;
  2777. {
  2778.     FILE *pfp, *fopen();
  2779.     char s[BUFSIZ], *strcpy();
  2780.  
  2781.     if ((pfp = fopen(pfile, "r")) == NULL)
  2782.     {
  2783.         fprintf(stderr,
  2784.         "Wheel cannot access Punctfile: %s\n", pfile);
  2785.         exit(1);
  2786.     }
  2787.     else
  2788.         while (fgets(s, BUFSIZ, pfp))
  2789.             strcpy(punctuation, s);
  2790. }
  2791.  
  2792. roll(fp)        /* roll word wheel through text */
  2793. FILE *fp;
  2794. {
  2795.     char word[BUFSIZ], wheel[20][BUFSIZ];
  2796.     int i;
  2797.  
  2798.     for (i = 0; i < 20; i++)
  2799.         *wheel[i] = NULL;
  2800.     while (getword(word, fp))
  2801.     {
  2802.         for (i = 0; i < notch-1; i++)
  2803.             strcpy(wheel[i], wheel[i+1]);
  2804.         strcpy(wheel[notch-1], word);
  2805.  
  2806.         if (*wheel[0] == NULL)
  2807.             continue;
  2808.         for (i = 0; i < notch; i++)
  2809.         {
  2810.             if (i != 0)
  2811.                 putchar(' ');
  2812.             fputs(wheel[i], stdout);
  2813.         }
  2814.         putchar('\n');
  2815.     }
  2816. }
  2817.  
  2818. getword(word, fp)    /* drives program through text word by word */
  2819. char word[];
  2820. FILE *fp;
  2821. {
  2822.     while ((*word = getc(fp)) && isskip(*word) && *word != EOF)
  2823.         ;
  2824.     if (*word == EOF)
  2825.         return(0);
  2826.     if (!nomap && isupper(*word))
  2827.         *word = tolower(*word);
  2828.  
  2829.     while ((*++word = getc(fp)) && !isskip(*word) && *word != EOF)
  2830.     {
  2831.         if (!nomap && isupper(*word))
  2832.             *word = tolower(*word);
  2833.     }
  2834.     *word = NULL;
  2835.     return(1);
  2836. }
  2837.  
  2838. isskip(c)        /* function to evaluate punctuation */
  2839. char c;
  2840. {
  2841.     char *ptr;
  2842.  
  2843.     if (isspace(c))
  2844.         return(1);
  2845.     for (ptr = punctuation; *ptr != c && *ptr != NULL; ptr++)
  2846.         ;
  2847.     if (*ptr == NULL)
  2848.         return(0);
  2849.     else
  2850.         return(1);
  2851. }
  2852. @@@ Fin de wheel.c
  2853. echo xref.c
  2854. cat >xref.c <<'@@@ Fin de xref.c'
  2855. # include <stdio.h>                /* xref.c (rev3.7) */
  2856. # include <ctype.h>
  2857. # include <signal.h>
  2858.  
  2859. usage()            /* print usage and synopsis of options */
  2860. {
  2861.     puts("Cross Reference Generator\t\t\t(rev3.7)");
  2862.     puts("Usage: xref [-r -ln -pn -ic -dPfile -] filename(s)");
  2863.     puts("-r : reset linenumber to 1 at beginning of every file");
  2864.     puts("-ln: line numbering begins with line n (instead of 1)");
  2865.     puts("-pn: page numbering begins with page n (instead of 1)");
  2866.     puts("-ic: page incrementer is character c (defaults to =)");
  2867.     puts("-wn: width of output page is n (defaults to 80)");
  2868.     puts("-d : define punctuation set according to Pfile");
  2869.     puts("-  : read text from standard input (terminal or pipe)");
  2870.     exit(1);
  2871. }
  2872.  
  2873. long lineno = 1;    /* count line numbers */
  2874. int resetno = 0;    /* reset lineno for new file */
  2875. char pgincr = '=';    /* page incrementing character */
  2876. int pageno = 0;        /* toggle and count page numbers */
  2877. int width = 70;        /* page width for entries */
  2878.  
  2879. char punctuation[512] = ",.;:-?!\"()[]{}" ;
  2880. char *tempfile = "/tmp/RefXXXXX";
  2881.  
  2882. main(argc, argv)    /* cross reference generator for a text */
  2883. int argc;
  2884. char *argv[];
  2885. {
  2886.     FILE *fp, *tfp, *fopen();
  2887.     char command[BUFSIZ], *mktemp(), *sprintf();
  2888.     int i, catch();
  2889.  
  2890.     if (argc == 1)
  2891.         usage();
  2892.     mktemp(tempfile);
  2893.     if (signal(SIGINT, SIG_IGN) != SIG_IGN)
  2894.         signal(SIGINT, catch);
  2895.     for (i = 1; i < argc; i++)
  2896.     {
  2897.         if (*argv[i] == '-')
  2898.             getflag(argv[i]);
  2899.         else if ((fp = fopen(argv[i], "r")) != NULL)
  2900.         {
  2901.             xref(fp);
  2902.             fclose(fp);
  2903.             if (resetno)
  2904.                 lineno = 1;
  2905.         }
  2906.         else  /* cannot open file */
  2907.         {
  2908.             fprintf(stderr,
  2909.             "Xref cannot access the file: %s\n", argv[i]);
  2910.             exit(1);
  2911.         }
  2912.     }
  2913.     sprintf(command, "sort %s -o %s", tempfile, tempfile);
  2914.     system(command);
  2915.     if ((tfp = fopen(tempfile, "r")) != NULL)
  2916.         frmt(tfp);
  2917.     unlink(tempfile);
  2918.     exit(0);
  2919. }
  2920.  
  2921. getflag(f)        /* parses command line to set options */
  2922. char *f;
  2923. {
  2924.     char *pfile;
  2925.     long atol();
  2926.  
  2927.     f++;
  2928.     switch(*f++)
  2929.     {
  2930.         case 'r':
  2931.             resetno = 1;
  2932.             break;
  2933.         case 'l':
  2934.             lineno = atol(f);
  2935.             break;
  2936.         case 'p':
  2937.             pageno = atoi(f);
  2938.             break;
  2939.         case 'i':
  2940.             pgincr = *f;
  2941.             break;
  2942.         case 'd':
  2943.             pfile = f;
  2944.             getpunct(pfile);
  2945.             break;
  2946.         case 'w':
  2947.             width = atoi(f) - 10;
  2948.             break;
  2949.         case NULL:
  2950.             xref(stdin);
  2951.             break;
  2952.         default:
  2953.             fprintf(stderr,
  2954.             "Invalid xref flag: -%s\n", --f);
  2955.             exit(1);
  2956.             break;
  2957.     }
  2958. }
  2959.  
  2960. getpunct(pfile)        /* read user's punctuation from pfile */
  2961. char *pfile;
  2962. {
  2963.     FILE *pfp, *fopen();
  2964.     char s[512], *strcpy();
  2965.  
  2966.     if ((pfp = fopen(pfile, "r")) == NULL)
  2967.     {
  2968.         fprintf(stderr,
  2969.         "Xref cannot access Punctfile: %s\n", pfile);
  2970.         exit(1);
  2971.     }
  2972.     else
  2973.         while (fgets(s, 512, pfp))
  2974.             strcpy(punctuation, s);
  2975. }
  2976.  
  2977. xref(fp)        /* prints cross reference for each word */
  2978. FILE *fp;
  2979. {
  2980.     FILE *tfp, *fopen();
  2981.     char word[BUFSIZ];
  2982.  
  2983.     if ((tfp = fopen(tempfile, "a")) == NULL)
  2984.     {
  2985.         perror(tempfile);
  2986.         exit(1);
  2987.     }
  2988.     while (getword(word, fp))
  2989.     {
  2990.         fprintf(tfp, "%s\002", word);  /* ctrl-b */
  2991.  
  2992.         if (pageno)
  2993.             fprintf(tfp, "%3d,%2ld; \n", pageno, lineno);
  2994.         else
  2995.             fprintf(tfp, "%5ld; \n", lineno);
  2996.     }
  2997.     fclose(tfp);
  2998. }
  2999.  
  3000. getword(word, fp)    /* drives program through text word by word */
  3001. char word[];
  3002. FILE *fp;
  3003. {
  3004.     static char nl = 0;
  3005.  
  3006.     if (nl)        /* increments lineno at beginning of line */
  3007.     {
  3008.         nl = 0;
  3009.         lineno++;
  3010.     }
  3011.     while ((*word = getc(fp)) && isskip(*word) && *word != EOF)
  3012.         if (*word == '\n')    /* skip to text */
  3013.             lineno++;
  3014.  
  3015.     if (*word == pgincr)
  3016.     {
  3017.         pageno++;    /* pgincr must begin word */
  3018.         lineno = 0;
  3019.     }
  3020.     if (*word == EOF)
  3021.         return(0);
  3022.     if (isupper(*word))
  3023.         *word = tolower(*word);
  3024.  
  3025.     while ((*++word = getc(fp)) && !isskip(*word) && *word !=EOF)
  3026.         if (isupper(*word))
  3027.             *word = tolower(*word);
  3028.  
  3029.     if (*word == '\n')    /* set nl at end of line */
  3030.         nl = 1;
  3031.     *word = NULL;
  3032.  
  3033.     return(1);
  3034. }
  3035.  
  3036. isskip(c)        /* function to evaluate punctuation */
  3037. char c;
  3038. {
  3039.     char *ptr;
  3040.  
  3041.     if (isspace(c))
  3042.         return(1);
  3043.     for (ptr = punctuation; *ptr != c && *ptr != NULL; ptr++)
  3044.         ;
  3045.     if (*ptr == NULL)
  3046.         return(0);
  3047.     else
  3048.         return(1);
  3049. }
  3050.  
  3051. frmt(fp)        /* format keywords and line numbers */
  3052. FILE *fp;
  3053. {
  3054.     char s[BUFSIZ], entry[BUFSIZ], *initial = "~~~~~";
  3055.     char old[BUFSIZ], new[BUFSIZ], ref[BUFSIZ], *strcat();
  3056.     int i, j;
  3057.  
  3058.     strcpy(old, initial);  /* make sure 1st kwd printed */
  3059.     *entry = NULL;
  3060.  
  3061.     while (fgets(s, BUFSIZ, fp))
  3062.     {
  3063.         for (i = 0; s[i] != '\002' && i < 510; i++)
  3064.             new[i] = s[i];
  3065.         new[i] = NULL;
  3066.  
  3067.         for (++i, j = 0; s[i] != '\n' && i < 510; i++, j++)
  3068.             ref[j] = s[i];
  3069.         ref[j] = NULL;
  3070.  
  3071.         if (strcmp(new, old) != 0)
  3072.         {
  3073.             if (*entry != NULL)
  3074.                 printf("%s\n", entry);
  3075.             entry[0] = ' ';
  3076.             entry[1] = NULL;
  3077.             printf("%s\n", new);
  3078.         }
  3079.         if (strlen(entry) < width)
  3080.             strcat(entry, ref);
  3081.         else  /* build entry */
  3082.         {
  3083.             printf("%s\n", entry);
  3084.             entry[0] = ' ';
  3085.             entry[1] = NULL;
  3086.             strcat(entry, ref);
  3087.         }
  3088.         strcpy(old, new);
  3089.     }
  3090.     printf("%s\n", entry);
  3091. }
  3092.  
  3093. catch()            /* remove tempfile in case of interrupt */
  3094. {
  3095.     unlink(tempfile);
  3096.     fprintf(stderr, "\nInterrupt\n");
  3097.     exit(1);
  3098. }
  3099. @@@ Fin de xref.c
  3100. echo ztext
  3101. cat >ztext <<'@@@ Fin de ztext'
  3102. All sounds of the shore were gone--
  3103. wing beats of pelicans,
  3104. the low wash of the waves,
  3105. the skim sizzling thru the drift,
  3106. the waving of palm wings on the breeze that shuffles terns,
  3107. the chaotic song of the canary trees--
  3108. all these were missing,
  3109. as if the storm on the horizon,
  3110. breathing, had drawn them in.
  3111. @@@ Fin de ztext
  3112. exit 0
  3113.